Search in sources :

Example 6 with Directive

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

the class SchemaGenerator method buildDirectives.

private GraphQLDirective[] buildDirectives(List<Directive> directives, List<Directive> extensionDirectives, DirectiveLocation directiveLocation) {
    directives = directives == null ? emptyList() : directives;
    extensionDirectives = extensionDirectives == null ? emptyList() : extensionDirectives;
    Set<String> names = new HashSet<>();
    List<GraphQLDirective> output = new ArrayList<>();
    for (Directive directive : directives) {
        if (!names.contains(directive.getName())) {
            names.add(directive.getName());
            output.add(schemaGeneratorHelper.buildDirective(directive, directiveLocation));
        }
    }
    for (Directive directive : extensionDirectives) {
        if (!names.contains(directive.getName())) {
            names.add(directive.getName());
            output.add(schemaGeneratorHelper.buildDirective(directive, directiveLocation));
        }
    }
    return output.toArray(new GraphQLDirective[0]);
}
Also used : ArrayList(java.util.ArrayList) GraphQLDirective(graphql.schema.GraphQLDirective) GraphQLDirective(graphql.schema.GraphQLDirective) Directive(graphql.language.Directive) HashSet(java.util.HashSet)

Example 7 with Directive

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

the class SchemaDiff method checkDirectives.

void checkDirectives(DiffCtx ctx, TypeDefinition old, List<Directive> oldDirectives, List<Directive> newDirectives) {
    if (!options.enforceDirectives) {
        return;
    }
    Map<String, Directive> oldDirectivesMap = sortedMap(oldDirectives, Directive::getName);
    Map<String, Directive> newDirectivesMap = sortedMap(newDirectives, Directive::getName);
    for (String directiveName : oldDirectivesMap.keySet()) {
        Directive oldDirective = oldDirectivesMap.get(directiveName);
        Optional<Directive> newDirective = Optional.ofNullable(newDirectivesMap.get(directiveName));
        if (!newDirective.isPresent()) {
            ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName).reasonMsg("The new API does not have a directive named '%s' on type '%s'", directiveName, old.getName()).build());
            continue;
        }
        Map<String, Argument> oldArgumentsByName = new TreeMap<>(oldDirective.getArgumentsByName());
        Map<String, Argument> newArgumentsByName = new TreeMap<>(newDirective.get().getArgumentsByName());
        if (oldArgumentsByName.size() > newArgumentsByName.size()) {
            ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName).reasonMsg("The new API has less arguments on directive '%s' on type '%s' than the old API", directiveName, old.getName()).build());
            return;
        }
        for (String argName : oldArgumentsByName.keySet()) {
            Argument oldArgument = oldArgumentsByName.get(argName);
            Optional<Argument> newArgument = Optional.ofNullable(newArgumentsByName.get(argName));
            if (!newArgument.isPresent()) {
                ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName, argName).reasonMsg("The new API does not have an argument named '%s' on directive '%s' on type '%s'", argName, directiveName, old.getName()).build());
            } else {
                Value oldValue = oldArgument.getValue();
                Value newValue = newArgument.get().getValue();
                if (oldValue != null && newValue != null) {
                    if (!oldValue.getClass().equals(newValue.getClass())) {
                        ctx.report(DiffEvent.apiBreakage().category(DiffCategory.INVALID).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName, argName).reasonMsg("The new API has changed value types on argument named '%s' on directive '%s' on type '%s'", argName, directiveName, old.getName()).build());
                    }
                }
            }
        }
    }
}
Also used : Argument(graphql.language.Argument) Value(graphql.language.Value) TreeMap(java.util.TreeMap) Directive(graphql.language.Directive)

Example 8 with Directive

use of graphql.language.Directive in project timbuctoo by HuygensING.

the class RdfWiringFactory method getDataFetcher.

@Override
public DataFetcher getDataFetcher(FieldWiringEnvironment environment) {
    if (environment.getFieldDefinition().getDirective("passThrough") != null) {
        return DataFetchingEnvironment::getSource;
    } else if (environment.getFieldDefinition().getDirective("related") != null) {
        final Directive directive = environment.getFieldDefinition().getDirective("related");
        String source = ((StringValue) directive.getArgument("source").getValue()).getValue();
        String predicate = ((StringValue) directive.getArgument("predicate").getValue()).getValue();
        String direction = ((StringValue) directive.getArgument("direction").getValue()).getValue();
        return new CollectionFetcherWrapper(argumentsHelper, new RelationsOfSubjectDataFetcher(source, predicate, Direction.valueOf(direction)));
    } else if (environment.getFieldDefinition().getDirective("fromCollection") != null) {
        final Directive directive = environment.getFieldDefinition().getDirective("fromCollection");
        String uri = ((StringValue) directive.getArgument("uri").getValue()).getValue();
        boolean listAll = ((BooleanValue) directive.getArgument("listAll").getValue()).isValue();
        if (listAll) {
            return new CollectionFetcherWrapper(argumentsHelper, new CollectionDataFetcher(uri));
        } else {
            return lookupFetcher;
        }
    } else if (environment.getFieldDefinition().getDirective("rdf") != null) {
        final Directive directive = environment.getFieldDefinition().getDirective("rdf");
        String uri = ((StringValue) directive.getArgument("predicate").getValue()).getValue();
        Direction direction = valueOf(((StringValue) directive.getArgument("direction").getValue()).getValue());
        boolean isList = ((BooleanValue) directive.getArgument("isList").getValue()).isValue();
        boolean isObject = ((BooleanValue) directive.getArgument("isObject").getValue()).isValue();
        boolean isValue = ((BooleanValue) directive.getArgument("isValue").getValue()).isValue();
        if (isObject && isValue) {
            return new DataFetcherWrapper(argumentsHelper, isList, new UnionDataFetcher(uri, direction));
        } else {
            if (isObject) {
                return new DataFetcherWrapper(argumentsHelper, isList, new RelationDataFetcher(uri, direction));
            } else {
                return new DataFetcherWrapper(argumentsHelper, isList, new TypedLiteralDataFetcher(uri));
            }
        }
    } else if (environment.getFieldDefinition().getDirective("uri") != null) {
        return uriFetcher;
    } else if (environment.getFieldDefinition().getDirective("dataSet") != null) {
        final Directive directive = environment.getFieldDefinition().getDirective("dataSet");
        String userId = ((StringValue) directive.getArgument("userId").getValue()).getValue();
        String dataSetId = ((StringValue) directive.getArgument("dataSetId").getValue()).getValue();
        final DataSet dataSet = dataSetRepository.unsafeGetDataSetWithoutCheckingPermissions(userId, dataSetId).orElse(null);
        return dataFetchingEnvironment -> new DatabaseResult() {

            @Override
            public DataSet getDataSet() {
                return dataSet;
            }
        };
    } else if (environment.getFieldDefinition().getDirective("entityTitle") != null) {
        return entityTitleFetcher;
    } else if (environment.getFieldDefinition().getDirective("entityDescription") != null) {
        return entityDescriptionFetcher;
    } else if (environment.getFieldDefinition().getDirective("entityImage") != null) {
        return entityImageFetcher;
    }
    return null;
}
Also used : CollectionDataFetcher(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.berkeleydb.datafetchers.CollectionDataFetcher) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) DatabaseResult(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DatabaseResult) RelationsOfSubjectDataFetcher(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.berkeleydb.datafetchers.RelationsOfSubjectDataFetcher) TypedLiteralDataFetcher(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.berkeleydb.datafetchers.TypedLiteralDataFetcher) Direction(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction) BooleanValue(graphql.language.BooleanValue) UnionDataFetcher(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.berkeleydb.datafetchers.UnionDataFetcher) StringValue(graphql.language.StringValue) Directive(graphql.language.Directive) RelationDataFetcher(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.berkeleydb.datafetchers.RelationDataFetcher)

Example 9 with Directive

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

the class SchemaTypeChecker method checkEnumValues.

private void checkEnumValues(List<GraphQLError> errors, EnumTypeDefinition enumType, List<EnumValueDefinition> enumValueDefinitions) {
    // enum unique ness
    checkNamedUniqueness(errors, enumValueDefinitions, EnumValueDefinition::getName, (name, inputValueDefinition) -> new NonUniqueNameError(enumType, inputValueDefinition));
    // directive checks
    enumValueDefinitions.forEach(enumValue -> {
        BiFunction<String, Directive, NonUniqueDirectiveError> errorFunction = (directiveName, directive) -> new NonUniqueDirectiveError(enumType, enumValue, directiveName);
        checkNamedUniqueness(errors, enumValue.getDirectives(), Directive::getName, errorFunction);
    });
    enumValueDefinitions.forEach(enumValue -> enumValue.getDirectives().forEach(directive -> {
        checkDeprecatedDirective(errors, directive, () -> new InvalidDeprecationDirectiveError(enumType, enumValue));
        BiFunction<String, Argument, NonUniqueArgumentError> errorFunction = (argumentName, argument) -> new NonUniqueArgumentError(enumType, enumValue, argumentName);
        checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, errorFunction);
    }));
}
Also used : MissingInterfaceTypeError(graphql.schema.idl.errors.MissingInterfaceTypeError) BiFunction(java.util.function.BiFunction) InputValueDefinition(graphql.language.InputValueDefinition) SchemaDefinition(graphql.language.SchemaDefinition) MissingInterfaceFieldError(graphql.schema.idl.errors.MissingInterfaceFieldError) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Type(graphql.language.Type) 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) QueryOperationMissingError(graphql.schema.idl.errors.QueryOperationMissingError) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) InterfaceFieldRedefinitionError(graphql.schema.idl.errors.InterfaceFieldRedefinitionError) Predicate(java.util.function.Predicate) Collection(java.util.Collection) FieldDefinition(graphql.language.FieldDefinition) Set(java.util.Set) TypeDefinition(graphql.language.TypeDefinition) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) AstPrinter(graphql.language.AstPrinter) MissingTypeError(graphql.schema.idl.errors.MissingTypeError) List(java.util.List) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) InterfaceFieldArgumentRedefinitionError(graphql.schema.idl.errors.InterfaceFieldArgumentRedefinitionError) SchemaMissingError(graphql.schema.idl.errors.SchemaMissingError) NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) Internal(graphql.Internal) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) MissingTypeResolverError(graphql.schema.idl.errors.MissingTypeResolverError) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UnionTypeDefinition(graphql.language.UnionTypeDefinition) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError) OperationTypesMustBeObjects(graphql.schema.idl.errors.OperationTypesMustBeObjects) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) MissingInterfaceFieldArgumentsError(graphql.schema.idl.errors.MissingInterfaceFieldArgumentsError) Directive(graphql.language.Directive) Consumer(java.util.function.Consumer) Argument(graphql.language.Argument) StringValue(graphql.language.StringValue) Collections(java.util.Collections) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) BiFunction(java.util.function.BiFunction) EnumValueDefinition(graphql.language.EnumValueDefinition) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError) Directive(graphql.language.Directive)

Example 10 with Directive

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

the class SchemaTypeExtensionsChecker method checkInputObjectTypeExtensions.

/*
     * Input object type extensions have the potential to be invalid if incorrectly defined.
     *
     * The named type must already be defined and must be a Input Object type.
     * All fields of an Input Object type extension must have unique names.
     * All fields of an Input Object type extension must not already be a field of the original Input Object.
     * Any directives provided must not already apply to the original Input Object type.
     */
private void checkInputObjectTypeExtensions(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) {
    typeRegistry.inputObjectTypeExtensions().forEach((name, extensions) -> {
        checkTypeExtensionHasCorrespondingType(errors, typeRegistry, name, extensions, InputObjectTypeDefinition.class);
        checkTypeExtensionDirectiveRedefinition(errors, typeRegistry, name, extensions, InputObjectTypeDefinition.class);
        // field redefinitions
        extensions.forEach(extension -> {
            List<InputValueDefinition> inputValueDefinitions = extension.getInputValueDefinitions();
            // field unique ness
            checkNamedUniqueness(errors, inputValueDefinitions, InputValueDefinition::getName, (namedField, fieldDef) -> new NonUniqueNameError(extension, fieldDef));
            // directive checks
            inputValueDefinitions.forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName, (directiveName, directive) -> new NonUniqueDirectiveError(extension, fld, directiveName)));
            inputValueDefinitions.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 -> checkForInputValueRedefinition(errors, otherTypeExt, otherTypeExt.getInputValueDefinitions(), inputValueDefinitions));
            // 
            // then check for field re-defs from the base type
            Optional<InputObjectTypeDefinition> baseTypeOpt = typeRegistry.getType(extension.getName(), InputObjectTypeDefinition.class);
            baseTypeOpt.ifPresent(baseTypeDef -> checkForInputValueRedefinition(errors, extension, inputValueDefinitions, baseTypeDef.getInputValueDefinitions()));
        });
    });
}
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) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) InputValueDefinition(graphql.language.InputValueDefinition) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError)

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