Search in sources :

Example 1 with DirectiveDefinition

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();
}
Also used : TypeName(graphql.language.TypeName) SchemaDefinition(graphql.language.SchemaDefinition) Document(graphql.language.Document) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) ArrayList(java.util.ArrayList) List(java.util.List) DirectiveDefinition(graphql.language.DirectiveDefinition) Map(java.util.Map)

Example 2 with DirectiveDefinition

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DirectiveDefinition(graphql.language.DirectiveDefinition) GraphQLDirective(graphql.schema.GraphQLDirective)

Example 3 with DirectiveDefinition

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);
}
Also used : Internal(graphql.Internal) Node(graphql.language.Node) INPUT_FIELD_DEFINITION(graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION) DirectiveLocation(graphql.introspection.Introspection.DirectiveLocation) FIELD_DEFINITION(graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InputValueDefinition(graphql.language.InputValueDefinition) DirectiveDefinition(graphql.language.DirectiveDefinition) SchemaDefinition(graphql.language.SchemaDefinition) IllegalNameError(graphql.schema.idl.errors.IllegalNameError) ARGUMENT_DEFINITION(graphql.introspection.Introspection.DirectiveLocation.ARGUMENT_DEFINITION) UNION(graphql.introspection.Introspection.DirectiveLocation.UNION) ENUM(graphql.introspection.Introspection.DirectiveLocation.ENUM) EnumTypeDefinition(graphql.language.EnumTypeDefinition) DirectiveUnknownArgumentError(graphql.schema.idl.errors.DirectiveUnknownArgumentError) INPUT_OBJECT(graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT) UnionTypeDefinition(graphql.language.UnionTypeDefinition) DirectiveMissingNonNullArgumentError(graphql.schema.idl.errors.DirectiveMissingNonNullArgumentError) EnumValueDefinition(graphql.language.EnumValueDefinition) DirectiveIllegalReferenceError(graphql.schema.idl.errors.DirectiveIllegalReferenceError) OBJECT(graphql.introspection.Introspection.DirectiveLocation.OBJECT) GraphQLError(graphql.GraphQLError) Map(java.util.Map) TypeName(graphql.language.TypeName) DirectiveUndeclaredError(graphql.schema.idl.errors.DirectiveUndeclaredError) FpKit.mergeFirst(graphql.util.FpKit.mergeFirst) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) SCALAR(graphql.introspection.Introspection.DirectiveLocation.SCALAR) INTERFACE(graphql.introspection.Introspection.DirectiveLocation.INTERFACE) NotAnInputTypeError(graphql.schema.idl.errors.NotAnInputTypeError) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) Collection(java.util.Collection) FieldDefinition(graphql.language.FieldDefinition) TypeDefinition(graphql.language.TypeDefinition) Collectors(java.util.stream.Collectors) Directive(graphql.language.Directive) Argument(graphql.language.Argument) MissingTypeError(graphql.schema.idl.errors.MissingTypeError) List(java.util.List) ENUM_VALUE(graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) NamedNode(graphql.language.NamedNode) Optional(java.util.Optional) FpKit.getByName(graphql.util.FpKit.getByName) NonNullType(graphql.language.NonNullType) DirectiveIllegalLocationError(graphql.schema.idl.errors.DirectiveIllegalLocationError) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) SchemaDefinition(graphql.language.SchemaDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) DirectiveDefinition(graphql.language.DirectiveDefinition) Directive(graphql.language.Directive)

Example 4 with DirectiveDefinition

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);
    }));
}
Also used : Internal(graphql.Internal) Node(graphql.language.Node) MissingInterfaceTypeError(graphql.schema.idl.errors.MissingInterfaceTypeError) INPUT_FIELD_DEFINITION(graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION) BiFunction(java.util.function.BiFunction) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InputValueDefinition(graphql.language.InputValueDefinition) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) DirectiveDefinition(graphql.language.DirectiveDefinition) MissingTypeResolverError(graphql.schema.idl.errors.MissingTypeResolverError) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Introspection(graphql.introspection.Introspection) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Type(graphql.language.Type) UnionTypeDefinition(graphql.language.UnionTypeDefinition) MissingScalarImplementationError(graphql.schema.idl.errors.MissingScalarImplementationError) EnumValueDefinition(graphql.language.EnumValueDefinition) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) GraphQLError(graphql.GraphQLError) Map(java.util.Map) TypeName(graphql.language.TypeName) LinkedHashSet(java.util.LinkedHashSet) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError) Predicate(java.util.function.Predicate) Collection(java.util.Collection) FieldDefinition(graphql.language.FieldDefinition) Set(java.util.Set) TypeDefinition(graphql.language.TypeDefinition) Directive(graphql.language.Directive) Consumer(java.util.function.Consumer) Argument(graphql.language.Argument) MissingTypeError(graphql.schema.idl.errors.MissingTypeError) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) DirectiveIllegalLocationError(graphql.schema.idl.errors.DirectiveIllegalLocationError) Collections(java.util.Collections) BiFunction(java.util.function.BiFunction) EnumValueDefinition(graphql.language.EnumValueDefinition) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError)

Example 5 with DirectiveDefinition

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()));
        });
    });
}
Also used : Internal(graphql.Internal) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InputValueDefinition(graphql.language.InputValueDefinition) DirectiveDefinition(graphql.language.DirectiveDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) TypeExtensionFieldRedefinitionError(graphql.schema.idl.errors.TypeExtensionFieldRedefinitionError) UnionTypeDefinition(graphql.language.UnionTypeDefinition) EnumValueDefinition(graphql.language.EnumValueDefinition) GraphQLError(graphql.GraphQLError) Map(java.util.Map) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) TypeExtensionEnumValueRedefinitionError(graphql.schema.idl.errors.TypeExtensionEnumValueRedefinitionError) TypeExtensionMissingBaseTypeError(graphql.schema.idl.errors.TypeExtensionMissingBaseTypeError) TypeName(graphql.language.TypeName) SchemaTypeChecker.checkNamedUniqueness(graphql.schema.idl.SchemaTypeChecker.checkNamedUniqueness) FpKit.mergeFirst(graphql.util.FpKit.mergeFirst) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError) FieldDefinition(graphql.language.FieldDefinition) TypeDefinition(graphql.language.TypeDefinition) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Argument(graphql.language.Argument) MissingTypeError(graphql.schema.idl.errors.MissingTypeError) List(java.util.List) FpKit(graphql.util.FpKit) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) Argument(graphql.language.Argument) FieldDefinition(graphql.language.FieldDefinition) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError)

Aggregations

DirectiveDefinition (graphql.language.DirectiveDefinition)14 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)11 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)11 TypeDefinition (graphql.language.TypeDefinition)11 UnionTypeDefinition (graphql.language.UnionTypeDefinition)11 GraphQLError (graphql.GraphQLError)10 EnumTypeDefinition (graphql.language.EnumTypeDefinition)10 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)10 InputValueDefinition (graphql.language.InputValueDefinition)10 TypeName (graphql.language.TypeName)10 List (java.util.List)10 Map (java.util.Map)10 Internal (graphql.Internal)9 Argument (graphql.language.Argument)9 EnumValueDefinition (graphql.language.EnumValueDefinition)9 FieldDefinition (graphql.language.FieldDefinition)9 MissingTypeError (graphql.schema.idl.errors.MissingTypeError)9 NonUniqueNameError (graphql.schema.idl.errors.NonUniqueNameError)9 Optional (java.util.Optional)9 NonUniqueArgumentError (graphql.schema.idl.errors.NonUniqueArgumentError)8