use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class IntrospectionResultToSchema method createSchemaDefinition.
/**
* Returns a IDL Document that reprSesents the schema as defined by the introspection result map
*
* @param introspectionResult the result of an introspection query on a schema
*
* @return a IDL Document of the schema
*/
@SuppressWarnings("unchecked")
public Document createSchemaDefinition(Map<String, Object> introspectionResult) {
assertTrue(introspectionResult.get("__schema") != null, "__schema expected");
Map<String, Object> schema = (Map<String, Object>) introspectionResult.get("__schema");
Map<String, Object> queryType = (Map<String, Object>) schema.get("queryType");
assertNotNull(queryType, "queryType expected");
TypeName query = new TypeName((String) queryType.get("name"));
boolean nonDefaultQueryName = !"Query".equals(query.getName());
SchemaDefinition schemaDefinition = new SchemaDefinition();
schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("query", query));
Map<String, Object> mutationType = (Map<String, Object>) schema.get("mutationType");
boolean nonDefaultMutationName = false;
if (mutationType != null) {
TypeName mutation = new TypeName((String) mutationType.get("name"));
nonDefaultMutationName = !"Mutation".equals(mutation.getName());
schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("mutation", mutation));
}
Map<String, Object> subscriptionType = (Map<String, Object>) schema.get("subscriptionType");
boolean nonDefaultSubscriptionName = false;
if (subscriptionType != null) {
TypeName subscription = new TypeName((String) subscriptionType.get("name"));
nonDefaultSubscriptionName = !"Subscription".equals(subscription.getName());
schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("subscription", subscription));
}
Document document = new Document();
if (nonDefaultQueryName || nonDefaultMutationName || nonDefaultSubscriptionName) {
document.getDefinitions().add(schemaDefinition);
}
List<Map<String, Object>> types = (List<Map<String, Object>>) schema.get("types");
for (Map<String, Object> type : types) {
TypeDefinition typeDefinition = createTypeDefinition(type);
if (typeDefinition == null)
continue;
document.getDefinitions().add(typeDefinition);
}
return document;
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaTypeChecker method checkObjTypeFields.
private void checkObjTypeFields(List<GraphQLError> errors, ObjectTypeDefinition typeDefinition, List<FieldDefinition> fieldDefinitions) {
// field unique ness
checkNamedUniqueness(errors, fieldDefinitions, FieldDefinition::getName, (name, fieldDef) -> new NonUniqueNameError(typeDefinition, fieldDef));
// field arg unique ness
fieldDefinitions.forEach(fld -> checkNamedUniqueness(errors, fld.getInputValueDefinitions(), InputValueDefinition::getName, (name, inputValueDefinition) -> new NonUniqueArgumentError(typeDefinition, fld, name)));
// directive checks
fieldDefinitions.forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName, (directiveName, directive) -> new NonUniqueDirectiveError(typeDefinition, fld, directiveName)));
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
checkDeprecatedDirective(errors, directive, () -> new InvalidDeprecationDirectiveError(typeDefinition, fld));
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, (argumentName, argument) -> new NonUniqueArgumentError(typeDefinition, fld, argumentName));
}));
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaTypeExtensionsChecker method checkTypeExtensionHasCorrespondingType.
private void checkTypeExtensionHasCorrespondingType(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, String name, List<? extends TypeDefinition> extTypeList, Class<? extends TypeDefinition> targetClass) {
TypeDefinition extensionDefinition = extTypeList.get(0);
Optional<? extends TypeDefinition> typeDefinition = typeRegistry.getType(new TypeName(name), targetClass);
if (!typeDefinition.isPresent()) {
errors.add(new TypeExtensionMissingBaseTypeError(extensionDefinition));
}
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class TypeDefinitionRegistry method isPossibleType.
/**
* Returns true of the abstract type is in implemented by the object type
*
* @param abstractType the abstract type to check (interface or union)
* @param possibleObjectType the object type to check
*
* @return true if the object type implements the abstract type
*/
@SuppressWarnings("ConstantConditions")
public boolean isPossibleType(Type abstractType, Type possibleObjectType) {
if (!isInterfaceOrUnion(abstractType)) {
return false;
}
if (!isObjectType(possibleObjectType)) {
return false;
}
ObjectTypeDefinition targetObjectTypeDef = getType(possibleObjectType, ObjectTypeDefinition.class).get();
TypeDefinition abstractTypeDef = getType(abstractType).get();
if (abstractTypeDef instanceof UnionTypeDefinition) {
List<Type> memberTypes = ((UnionTypeDefinition) abstractTypeDef).getMemberTypes();
for (Type memberType : memberTypes) {
Optional<ObjectTypeDefinition> checkType = getType(memberType, ObjectTypeDefinition.class);
if (checkType.isPresent()) {
if (checkType.get().getName().equals(targetObjectTypeDef.getName())) {
return true;
}
}
}
return false;
} else {
InterfaceTypeDefinition iFace = (InterfaceTypeDefinition) abstractTypeDef;
List<ObjectTypeDefinition> objectTypeDefinitions = getImplementationsOf(iFace);
return objectTypeDefinitions.stream().anyMatch(od -> od.getName().equals(targetObjectTypeDef.getName()));
}
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaGenerator method buildOutputType.
/**
* This is the main recursive spot that builds out the various forms of Output types
*
* @param buildCtx the context we need to work out what we are doing
* @param rawType the type to be built
*
* @return an output type
*/
@SuppressWarnings("unchecked")
private <T extends GraphQLOutputType> T buildOutputType(BuildContext buildCtx, Type rawType) {
TypeDefinition typeDefinition = buildCtx.getTypeDefinition(rawType);
TypeInfo typeInfo = TypeInfo.typeInfo(rawType);
GraphQLOutputType outputType = buildCtx.hasOutputType(typeDefinition);
if (outputType != null) {
return typeInfo.decorate(outputType);
}
if (buildCtx.stackContains(typeInfo)) {
// otherwise we will go into an infinite loop
return typeInfo.decorate(typeRef(typeInfo.getName()));
}
buildCtx.push(typeInfo);
if (typeDefinition instanceof ObjectTypeDefinition) {
outputType = buildObjectType(buildCtx, (ObjectTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof InterfaceTypeDefinition) {
outputType = buildInterfaceType(buildCtx, (InterfaceTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof UnionTypeDefinition) {
outputType = buildUnionType(buildCtx, (UnionTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof EnumTypeDefinition) {
outputType = buildEnumType(buildCtx, (EnumTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof ScalarTypeDefinition) {
outputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
} else {
// typeDefinition is not a valid output type
throw new NotAnOutputTypeError(typeDefinition);
}
buildCtx.put(outputType);
buildCtx.pop();
return (T) typeInfo.decorate(outputType);
}
Aggregations