use of graphql.language.DirectiveDefinition in project graphql-java by graphql-java.
the class IntrospectionResultToSchema method createSchemaDefinition.
/**
* Returns a IDL Document that represents 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 = TypeName.newTypeName().name((String) queryType.get("name")).build();
boolean nonDefaultQueryName = !"Query".equals(query.getName());
SchemaDefinition.Builder schemaDefinition = SchemaDefinition.newSchemaDefinition();
schemaDefinition.description(toDescription(schema));
schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("query").typeName(query).build());
Map<String, Object> mutationType = (Map<String, Object>) schema.get("mutationType");
boolean nonDefaultMutationName = false;
if (mutationType != null) {
TypeName mutation = TypeName.newTypeName().name((String) mutationType.get("name")).build();
nonDefaultMutationName = !"Mutation".equals(mutation.getName());
schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("mutation").typeName(mutation).build());
}
Map<String, Object> subscriptionType = (Map<String, Object>) schema.get("subscriptionType");
boolean nonDefaultSubscriptionName = false;
if (subscriptionType != null) {
TypeName subscription = TypeName.newTypeName().name(((String) subscriptionType.get("name"))).build();
nonDefaultSubscriptionName = !"Subscription".equals(subscription.getName());
schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("subscription").typeName(subscription).build());
}
Document.Builder document = Document.newDocument();
if (nonDefaultQueryName || nonDefaultMutationName || nonDefaultSubscriptionName) {
document.definition(schemaDefinition.build());
}
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.definition(typeDefinition);
}
List<Map<String, Object>> directives = (List<Map<String, Object>>) schema.get("directives");
if (directives != null) {
for (Map<String, Object> directive : directives) {
DirectiveDefinition directiveDefinition = createDirective(directive);
if (directiveDefinition == null) {
continue;
}
document.definition(directiveDefinition);
}
}
return document.build();
}
use of graphql.language.DirectiveDefinition in project graphql-java by graphql-java.
the class SchemaGeneratorHelper method buildAdditionalDirectiveDefinitions.
Set<GraphQLDirective> buildAdditionalDirectiveDefinitions(BuildContext buildCtx) {
Set<GraphQLDirective> additionalDirectives = new LinkedHashSet<>();
TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry();
for (DirectiveDefinition directiveDefinition : typeRegistry.getDirectiveDefinitions().values()) {
GraphQLDirective directive = buildDirectiveDefinitionFromAst(buildCtx, directiveDefinition, inputTypeFactory(buildCtx));
buildCtx.addDirectiveDefinition(directive);
additionalDirectives.add(directive);
}
return additionalDirectives;
}
use of graphql.language.DirectiveDefinition in project graphql-java by graphql-java.
the class SchemaTypeDirectivesChecker method checkTypeDirectives.
void checkTypeDirectives(List<GraphQLError> errors) {
typeRegistry.objectTypeExtensions().values().forEach(extDefinitions -> extDefinitions.forEach(ext -> checkDirectives(OBJECT, errors, ext)));
typeRegistry.interfaceTypeExtensions().values().forEach(extDefinitions -> extDefinitions.forEach(ext -> checkDirectives(INTERFACE, errors, ext)));
typeRegistry.unionTypeExtensions().values().forEach(extDefinitions -> extDefinitions.forEach(ext -> checkDirectives(UNION, errors, ext)));
typeRegistry.enumTypeExtensions().values().forEach(extDefinitions -> extDefinitions.forEach(ext -> checkDirectives(ENUM, errors, ext)));
typeRegistry.scalarTypeExtensions().values().forEach(extDefinitions -> extDefinitions.forEach(ext -> checkDirectives(SCALAR, errors, ext)));
typeRegistry.inputObjectTypeExtensions().values().forEach(extDefinitions -> extDefinitions.forEach(ext -> checkDirectives(INPUT_OBJECT, errors, ext)));
typeRegistry.getTypes(ObjectTypeDefinition.class).forEach(typeDef -> checkDirectives(OBJECT, errors, typeDef));
typeRegistry.getTypes(InterfaceTypeDefinition.class).forEach(typeDef -> checkDirectives(INTERFACE, errors, typeDef));
typeRegistry.getTypes(UnionTypeDefinition.class).forEach(typeDef -> checkDirectives(UNION, errors, typeDef));
typeRegistry.getTypes(EnumTypeDefinition.class).forEach(typeDef -> checkDirectives(ENUM, errors, typeDef));
typeRegistry.getTypes(InputObjectTypeDefinition.class).forEach(typeDef -> checkDirectives(INPUT_OBJECT, errors, typeDef));
typeRegistry.scalars().values().forEach(typeDef -> checkDirectives(SCALAR, errors, typeDef));
List<Directive> schemaDirectives = SchemaExtensionsChecker.gatherSchemaDirectives(typeRegistry, errors);
// we need to have a Node for error reporting so we make one in case there is not one
SchemaDefinition schemaDefinition = typeRegistry.schemaDefinition().orElse(SchemaDefinition.newSchemaDefinition().build());
checkDirectives(DirectiveLocation.SCHEMA, errors, typeRegistry, schemaDefinition, "schema", schemaDirectives);
Collection<DirectiveDefinition> directiveDefinitions = typeRegistry.getDirectiveDefinitions().values();
commonCheck(directiveDefinitions, errors);
}
use of graphql.language.DirectiveDefinition in project graphql-java by graphql-java.
the class SchemaTypeChecker method checkEnumValues.
private void checkEnumValues(List<GraphQLError> errors, EnumTypeDefinition enumType, List<EnumValueDefinition> enumValueDefinitions, Map<String, DirectiveDefinition> directiveDefinitionMap) {
// enum unique ness
checkNamedUniqueness(errors, enumValueDefinitions, EnumValueDefinition::getName, (name, inputValueDefinition) -> new NonUniqueNameError(enumType, inputValueDefinition));
// directive checks
enumValueDefinitions.forEach(enumValue -> enumValue.getDirectives().forEach(directive -> {
BiFunction<String, Argument, NonUniqueArgumentError> errorFunction = (argumentName, argument) -> new NonUniqueArgumentError(enumType, enumValue, argumentName);
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, errorFunction);
}));
}
use of graphql.language.DirectiveDefinition in project graphql-java by graphql-java.
the class SchemaTypeExtensionsChecker method checkObjectTypeExtensions.
/*
* Object type extensions have the potential to be invalid if incorrectly defined.
*
* The named type must already be defined and must be an Object type.
* The fields of an Object type extension must have unique names; no two fields may share the same name.
* Any fields of an Object type extension must not be already defined on the original Object type.
* Any directives provided must not already apply to the original Object type.
* Any interfaces provided must not be already implemented by the original Object type.
* The resulting extended object type must be a super-set of all interfaces it implements.
*/
private void checkObjectTypeExtensions(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, Map<String, DirectiveDefinition> directiveDefinitionMap) {
typeRegistry.objectTypeExtensions().forEach((name, extensions) -> {
checkTypeExtensionHasCorrespondingType(errors, typeRegistry, name, extensions, ObjectTypeDefinition.class);
extensions.forEach(extension -> {
List<FieldDefinition> fieldDefinitions = extension.getFieldDefinitions();
// field unique ness
checkNamedUniqueness(errors, extension.getFieldDefinitions(), FieldDefinition::getName, (namedField, fieldDef) -> new NonUniqueNameError(extension, fieldDef));
// field arg unique ness
extension.getFieldDefinitions().forEach(fld -> checkNamedUniqueness(errors, fld.getInputValueDefinitions(), InputValueDefinition::getName, (namedField, inputValueDefinition) -> new NonUniqueArgumentError(extension, fld, name)));
// directive checks
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, (argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName))));
//
// fields must be unique within a type extension
forEachBut(extension, extensions, otherTypeExt -> checkForFieldRedefinition(errors, otherTypeExt, otherTypeExt.getFieldDefinitions(), fieldDefinitions));
//
// then check for field re-defs from the base type
Optional<ObjectTypeDefinition> baseTypeOpt = typeRegistry.getType(extension.getName(), ObjectTypeDefinition.class);
baseTypeOpt.ifPresent(baseTypeDef -> checkForFieldRedefinition(errors, extension, fieldDefinitions, baseTypeDef.getFieldDefinitions()));
});
});
}
Aggregations