private Conflict mkNotSameTypeError(String responseName, Field fieldA, Field fieldB, GraphQLType typeA, GraphQLType typeB) { String name1 = typeA != null ? typeA.getName() : "null"; String name2 = typeB != null ? typeB.getName() : "null"; String reason = format("%s: they return differing types %s and %s", responseName, name1, name2); return new Conflict(responseName, reason, fieldA, fieldB); }
private void assertTypeUniqueness(GraphQLType type, Map<String, GraphQLType> result) { GraphQLType existingType = result.get(type.getName()); // do we have an existing definition if (existingType != null) { // type references are ok if (!(existingType instanceof GraphQLTypeReference || type instanceof GraphQLTypeReference)) // object comparison here is deliberate if (existingType != type) { throw new AssertException(format("All types within a GraphQL schema must have unique names. No two provided types may have the same name.\n" + "No provided type may have a name which conflicts with any built in types (including Scalar and Introspection types).\n" + "You have redefined the type '%s' from being a '%s' to a '%s'", type.getName(), existingType.getClass().getSimpleName(), type.getClass().getSimpleName())); } } }
public UnresolvedTypeException(GraphQLOutputType interfaceOrUnionType, GraphQLType providedType) { this("Runtime Object type '" + providedType.getName() + "' is not a possible type for " + "'" + interfaceOrUnionType.getName() + "'.", interfaceOrUnionType); }
private boolean isIntrospectionType(GraphQLType type) { return !options.isIncludeIntrospectionTypes() && type.getName().startsWith("__"); }
private static String mkMessage(ExecutionStepInfo executionStepInfo, ExecutionPath path) { GraphQLType unwrappedTyped = executionStepInfo.getUnwrappedNonNullType(); if (executionStepInfo.hasParent()) { GraphQLType unwrappedParentType = executionStepInfo.getParent().getUnwrappedNonNullType(); return String.format("Cannot return null for non-nullable type: '%s' within parent '%s' (%s)", unwrappedTyped.getName(), unwrappedParentType.getName(), path); } return String.format("Cannot return null for non-nullable type: '%s' (%s)", unwrappedTyped.getName(), path); }
/** * Returns true if a specified concrete type is a possible type of a provided abstract type. * If the provided abstract type is: * - an interface, it checks whether the concrete type is one of its implementations. * - a union, it checks whether the concrete type is one of its possible types. * * @param abstractType abstract type either interface or union * @param concreteType concrete type * * @return true if possible type, false otherwise. */ public boolean isPossibleType(GraphQLType abstractType, GraphQLObjectType concreteType) { if (abstractType instanceof GraphQLInterfaceType) { return getImplementations((GraphQLInterfaceType) abstractType).stream() .map(GraphQLType::getName) .anyMatch(name -> concreteType.getName().equals(name)); } else if (abstractType instanceof GraphQLUnionType) { return ((GraphQLUnionType) abstractType).getTypes().stream() .map(GraphQLType::getName) .anyMatch(name -> concreteType.getName().equals(name)); } return assertShouldNeverHappen("Unsupported abstract type %s. Abstract types supported are Union and Interface.", abstractType.getName()); }
/** * This will return the type in graphql SDL format, eg [typeName!]! * * @param type the type in play * * @return the type in graphql SDL format, eg [typeName!]! */ public static String simplePrint(GraphQLType type) { StringBuilder sb = new StringBuilder(); if (isNonNull(type)) { sb.append(simplePrint(unwrapOne(type))); sb.append("!"); } else if (isList(type)) { sb.append("["); sb.append(simplePrint(unwrapOne(type))); sb.append("]"); } else { sb.append(type.getName()); } return sb.toString(); }
public Object buildValue(Value value, GraphQLType requiredType) { Object result = null; if (GraphQLTypeUtil.isNonNull(requiredType)) { requiredType = unwrapOne(requiredType); } if (value == null) { return null; } if (requiredType instanceof GraphQLScalarType) { result = parseLiteral(value, (GraphQLScalarType) requiredType); } else if (value instanceof EnumValue && requiredType instanceof GraphQLEnumType) { result = ((EnumValue) value).getName(); } else if (value instanceof ArrayValue && isList(requiredType)) { ArrayValue arrayValue = (ArrayValue) value; GraphQLType wrappedType = unwrapOne(requiredType); result = arrayValue.getValues().stream() .map(item -> this.buildValue(item, wrappedType)).collect(Collectors.toList()); } else if (value instanceof ObjectValue && requiredType instanceof GraphQLInputObjectType) { result = buildObjectValue((ObjectValue) value, (GraphQLInputObjectType) requiredType); } else if (!(value instanceof NullValue)) { assertShouldNeverHappen( "cannot build value of %s from %s", requiredType.getName(), String.valueOf(value)); } return result; }
@Override public void checkFragmentSpread(FragmentSpread fragmentSpread) { FragmentDefinition fragment = getValidationContext().getFragment(fragmentSpread.getName()); if (fragment == null) return; GraphQLType typeCondition = TypeFromAST.getTypeFromAST(getValidationContext().getSchema(), fragment.getTypeCondition()); GraphQLCompositeType parentType = getValidationContext().getParentType(); if (typeCondition == null || parentType == null) return; if (isValidTargetCompositeType(typeCondition) && isValidTargetCompositeType(parentType) && !doTypesOverlap(typeCondition, parentType)) { String message = String.format("Fragment %s cannot be spread here as objects of " + "type %s can never be of type %s", fragmentSpread.getName(), parentType.getName(), typeCondition.getName()); addError(ValidationErrorType.InvalidFragmentType, fragmentSpread.getSourceLocation(), message); } }
private Conflict mkNotSameTypeError(String responseName, Field fieldA, Field fieldB, GraphQLType typeA, GraphQLType typeB) { String name1 = typeA != null ? typeA.getName() : "null"; String name2 = typeB != null ? typeB.getName() : "null"; String reason = format("%s: they return differing types %s and %s", responseName, name1, name2); return new Conflict(responseName, reason, fieldA, fieldB); }
public GraphQLSchemaGenerator withAdditionalTypes(Collection<GraphQLType> additionalTypes) { additionalTypes.stream() .filter(this::isRealType) .forEach(type -> { if (this.additionalTypes.put(type.getName(), type) != null) { throw new ConfigurationException("Type name collision: multiple registered additional types are named '" + type.getName() + "'"); } }); return this; }
public GraphQLInterfaceType[] resolveInterfaceReferences(DfsContext dfsContext, List<GraphQLType> graphQLInterfaceTypes) { GraphQLInterfaceType[] interfaceTypes = new GraphQLInterfaceType[graphQLInterfaceTypes.size()]; for (int i = 0; i < graphQLInterfaceTypes.size(); i++) { GraphQLType type = graphQLInterfaceTypes.get(i); if (type instanceof GraphQLTypeReference) { Class<?> cls = objectNameTypeReferenceMap.get(type.getName()); type = objectTypeResolverMap.get(cls); } interfaceTypes[i] = (GraphQLInterfaceType) type; } return interfaceTypes; }
public UnresolvedTypeException(GraphQLOutputType interfaceOrUnionType, GraphQLType providedType) { this("Runtime Object type '" + providedType.getName() + "' is not a possible type for " + "'" + interfaceOrUnionType.getName() + "'.", interfaceOrUnionType); }
public static boolean isRelayConnectionType(GraphQLType type) { return type instanceof GraphQLObjectType && !type.getName().equals(CONNECTION) && type.getName().endsWith(CONNECTION) && ((GraphQLObjectType) type).getFieldDefinition(EDGES) != null && ((GraphQLObjectType) type).getFieldDefinition(PAGE_INFO) != null; }
private boolean isRealType(GraphQLType type) { // Reject introspection types return !(GraphQLUtils.isIntrospectionType(type) // Reject quasi-types || type instanceof GraphQLTypeReference || type instanceof GraphQLArgument || type instanceof GraphQLDirective // Reject root types || type.getName().equals(messageBundle.interpolate(queryRoot)) || type.getName().equals(messageBundle.interpolate(mutationRoot)) || type.getName().equals(messageBundle.interpolate(subscriptionRoot))); }
private static String mkMessage(ExecutionStepInfo executionStepInfo, ExecutionPath path) { GraphQLType unwrappedTyped = executionStepInfo.getUnwrappedNonNullType(); if (executionStepInfo.hasParent()) { GraphQLType unwrappedParentType = executionStepInfo.getParent().getUnwrappedNonNullType(); return String.format("Cannot return null for non-nullable type: '%s' within parent '%s' (%s)", unwrappedTyped.getName(), unwrappedParentType.getName(), path); } return String.format("Cannot return null for non-nullable type: '%s' (%s)", unwrappedTyped.getName(), path); }
public DataFetcher<String> typeFetcher(Map<String, TypeConfig> types) { return environment -> { String typeName = environment.getParentType().getName(); return (types.containsKey(typeName)) ? types.get(typeName).getId() : null; }; }
public static AnnotatedType getMappedType(GraphQLType type) { return DirectivesUtil.directiveWithArg(((GraphQLDirectiveContainer) type).getDirectives(), MAPPED_TYPE, TYPE) .map(arg -> (AnnotatedType) arg.getValue()) .orElseThrow(() -> new IllegalArgumentException("GraphQL type " + type.getName() + " does not have a mapped Java type")); }
public static boolean isRelayNodeInterface(GraphQLType node) { return node instanceof GraphQLInterfaceType && node.getName().equals(Relay.NODE) && ((GraphQLInterfaceType) node).getFieldDefinitions().size() == 1 && ((GraphQLInterfaceType) node).getFieldDefinition(GraphQLId.RELAY_ID_FIELD_NAME) != null && isRelayId(((GraphQLInterfaceType) node).getFieldDefinition(GraphQLId.RELAY_ID_FIELD_NAME)); }
@Override public String convertOutput(Object original, AnnotatedType type, ResolutionEnvironment resolutionEnvironment) { if (type.getAnnotation(GraphQLId.class).relayId()) { return resolutionEnvironment.globalEnvironment.relay.toGlobalId(resolutionEnvironment.parentType.getName(), resolutionEnvironment.valueMapper.toString(original)); } return resolutionEnvironment.valueMapper.toString(original); }