Search in sources :

Example 11 with Directive

use of graphql.language.Directive 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) {
    typeRegistry.objectTypeExtensions().forEach((name, extensions) -> {
        checkTypeExtensionHasCorrespondingType(errors, typeRegistry, name, extensions, ObjectTypeDefinition.class);
        checkTypeExtensionDirectiveRedefinition(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
            extension.getFieldDefinitions().forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName, (directiveName, directive) -> new NonUniqueDirectiveError(extension, fld, directiveName)));
            fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
                checkDeprecatedDirective(errors, directive, () -> new InvalidDeprecationDirectiveError(extension, fld));
                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 : NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) Internal(graphql.Internal) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InputValueDefinition(graphql.language.InputValueDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) TypeExtensionFieldRedefinitionError(graphql.schema.idl.errors.TypeExtensionFieldRedefinitionError) Type(graphql.language.Type) UnionTypeDefinition(graphql.language.UnionTypeDefinition) EnumValueDefinition(graphql.language.EnumValueDefinition) GraphQLError(graphql.GraphQLError) TypeExtensionDirectiveRedefinitionError(graphql.schema.idl.errors.TypeExtensionDirectiveRedefinitionError) 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) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) SchemaTypeChecker.checkDeprecatedDirective(graphql.schema.idl.SchemaTypeChecker.checkDeprecatedDirective) TypeDefinition(graphql.language.TypeDefinition) Collectors(java.util.stream.Collectors) Directive(graphql.language.Directive) Consumer(java.util.function.Consumer) Argument(graphql.language.Argument) AstPrinter(graphql.language.AstPrinter) 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) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) FieldDefinition(graphql.language.FieldDefinition) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError)

Example 12 with Directive

use of graphql.language.Directive in project graphql-java by graphql-java.

the class GraphqlAntlrToLanguage method visitDirective.

@Override
public Void visitDirective(GraphqlParser.DirectiveContext ctx) {
    Directive directive = new Directive(ctx.name().getText());
    newNode(directive, ctx);
    for (ContextEntry contextEntry : contextStack) {
        if (contextEntry.contextProperty == ContextProperty.Field) {
            ((Field) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.FragmentDefinition) {
            ((FragmentDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.FragmentSpread) {
            ((FragmentSpread) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.InlineFragment) {
            ((InlineFragment) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.OperationDefinition) {
            ((OperationDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.EnumValueDefinition) {
            ((EnumValueDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.FieldDefinition) {
            ((FieldDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.InputValueDefinition) {
            ((InputValueDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.InterfaceTypeDefinition) {
            ((InterfaceTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.EnumTypeDefinition) {
            ((EnumTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.ObjectTypeDefinition) {
            ((ObjectTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.ScalarTypeDefinition) {
            ((ScalarTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.UnionTypeDefinition) {
            ((UnionTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.InputObjectTypeDefinition) {
            ((InputObjectTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.SchemaDefinition) {
            ((SchemaDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        }
    }
    addContextProperty(ContextProperty.Directive, directive);
    super.visitDirective(ctx);
    popContext();
    return null;
}
Also used : ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) FragmentDefinition(graphql.language.FragmentDefinition) EnumValueDefinition(graphql.language.EnumValueDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InputValueDefinition(graphql.language.InputValueDefinition) Directive(graphql.language.Directive) InlineFragment(graphql.language.InlineFragment)

Example 13 with Directive

use of graphql.language.Directive in project graphql-java by graphql-java.

the class IntrospectionResultToSchema method createDeprecatedDirective.

private void createDeprecatedDirective(Map<String, Object> field, List<Directive> directives) {
    if ((Boolean) field.get("isDeprecated")) {
        String reason = (String) field.get("deprecationReason");
        if (reason == null) {
            // default according to spec
            reason = "No longer supported";
        }
        Argument reasonArg = new Argument("reason", new StringValue(reason));
        Directive deprecated = new Directive("deprecated", Collections.singletonList(reasonArg));
        directives.add(deprecated);
    }
}
Also used : Argument(graphql.language.Argument) StringValue(graphql.language.StringValue) Directive(graphql.language.Directive)

Aggregations

Directive (graphql.language.Directive)13 Argument (graphql.language.Argument)9 EnumTypeDefinition (graphql.language.EnumTypeDefinition)8 EnumValueDefinition (graphql.language.EnumValueDefinition)8 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)8 InputValueDefinition (graphql.language.InputValueDefinition)8 TypeName (graphql.language.TypeName)8 GraphQLError (graphql.GraphQLError)7 Internal (graphql.Internal)7 AstPrinter (graphql.language.AstPrinter)7 FieldDefinition (graphql.language.FieldDefinition)7 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)7 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)7 Type (graphql.language.Type)7 TypeDefinition (graphql.language.TypeDefinition)7 UnionTypeDefinition (graphql.language.UnionTypeDefinition)7 InvalidDeprecationDirectiveError (graphql.schema.idl.errors.InvalidDeprecationDirectiveError)7 MissingTypeError (graphql.schema.idl.errors.MissingTypeError)7 NonUniqueArgumentError (graphql.schema.idl.errors.NonUniqueArgumentError)7 NonUniqueDirectiveError (graphql.schema.idl.errors.NonUniqueDirectiveError)7