Search in sources :

Example 1 with GraphQLDirectiveContainer

use of graphql.schema.GraphQLDirectiveContainer in project graphql-java by graphql-java.

the class IntrospectionWithDirectivesSupport method addAppliedDirectives.

private GraphQLObjectType addAppliedDirectives(GraphQLObjectType originalType, GraphQLCodeRegistry.Builder codeRegistry, GraphQLObjectType appliedDirectiveType, GraphQLObjectType directiveArgumentType) {
    GraphQLObjectType objectType = originalType.transform(bld -> bld.field(fld -> fld.name("appliedDirectives").type(nonNull(list(nonNull(appliedDirectiveType))))));
    DataFetcher<?> df = env -> {
        Object source = env.getSource();
        GraphQLSchema schema = env.getGraphQLSchema();
        if (source instanceof GraphQLDirectiveContainer) {
            GraphQLDirectiveContainer type = env.getSource();
            List<GraphQLAppliedDirective> appliedDirectives = DirectivesUtil.toAppliedDirectives(type);
            return filterAppliedDirectives(schema, false, type, appliedDirectives);
        }
        if (source instanceof GraphQLSchema) {
            List<GraphQLAppliedDirective> appliedDirectives = DirectivesUtil.toAppliedDirectives(schema.getSchemaAppliedDirectives(), schema.getSchemaDirectives());
            return filterAppliedDirectives(schema, true, null, appliedDirectives);
        }
        return assertShouldNeverHappen("What directive containing element have we not considered? - %s", originalType);
    };
    DataFetcher<?> argsDF = env -> {
        final GraphQLAppliedDirective directive = env.getSource();
        // we only show directive arguments that have values set on them
        return directive.getArguments().stream().filter(arg -> arg.getArgumentValue().isSet());
    };
    DataFetcher<?> argValueDF = env -> {
        final GraphQLAppliedDirectiveArgument argument = env.getSource();
        InputValueWithState value = argument.getArgumentValue();
        Node<?> literal = ValuesResolver.valueToLiteral(value, argument.getType());
        return AstPrinter.printAst(literal);
    };
    codeRegistry.dataFetcher(coordinates(objectType, "appliedDirectives"), df);
    codeRegistry.dataFetcher(coordinates(appliedDirectiveType, "args"), argsDF);
    codeRegistry.dataFetcher(coordinates(directiveArgumentType, "value"), argValueDF);
    return objectType;
}
Also used : GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) Node(graphql.language.Node) SchemaTransformer(graphql.schema.SchemaTransformer) GraphQLString(graphql.Scalars.GraphQLString) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) TraversalControl(graphql.util.TraversalControl) ValuesResolver(graphql.execution.ValuesResolver) Introspection.__Type(graphql.introspection.Introspection.__Type) GraphQLType(graphql.schema.GraphQLType) TraverserContext(graphql.util.TraverserContext) GraphQLList.list(graphql.schema.GraphQLList.list) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective) InputValueWithState(graphql.schema.InputValueWithState) Introspection.__EnumValue(graphql.introspection.Introspection.__EnumValue) Assert.assertShouldNeverHappen(graphql.Assert.assertShouldNeverHappen) GraphQLNonNull.nonNull(graphql.schema.GraphQLNonNull.nonNull) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) DirectivesUtil(graphql.DirectivesUtil) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLDirective(graphql.schema.GraphQLDirective) ImmutableSet(com.google.common.collect.ImmutableSet) PublicSpi(graphql.PublicSpi) CONTINUE(graphql.util.TraversalControl.CONTINUE) Set(java.util.Set) GraphQLTypeVisitorStub(graphql.schema.GraphQLTypeVisitorStub) FieldCoordinates.coordinates(graphql.schema.FieldCoordinates.coordinates) Introspection.__Schema(graphql.introspection.Introspection.__Schema) AstPrinter(graphql.language.AstPrinter) Introspection.__InputValue(graphql.introspection.Introspection.__InputValue) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) PublicApi(graphql.PublicApi) Assert.assertNotNull(graphql.Assert.assertNotNull) GraphQLObjectType.newObject(graphql.schema.GraphQLObjectType.newObject) Introspection.__Field(graphql.introspection.Introspection.__Field) NotNull(org.jetbrains.annotations.NotNull) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) InputValueWithState(graphql.schema.InputValueWithState) Node(graphql.language.Node) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLAppliedDirectiveArgument(graphql.schema.GraphQLAppliedDirectiveArgument) GraphQLObjectType.newObject(graphql.schema.GraphQLObjectType.newObject) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GraphQLAppliedDirective(graphql.schema.GraphQLAppliedDirective) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 2 with GraphQLDirectiveContainer

use of graphql.schema.GraphQLDirectiveContainer in project graphql-java-extended-validation by graphql-java.

the class TargetedValidationRules method walkListArg.

private List<GraphQLError> walkListArg(ValidationRule rule, ValidationEnvironment validationEnvironment, GraphQLList argumentType, List<Object> objectList) {
    List<GraphQLError> errors = new ArrayList<>();
    GraphQLInputType listItemType = Util.unwrapOneAndAllNonNull(argumentType);
    List<GraphQLDirective> directives;
    if (!(listItemType instanceof GraphQLDirectiveContainer)) {
        directives = Collections.emptyList();
    } else {
        directives = ((GraphQLDirectiveContainer) listItemType).getDirectives();
    }
    int ix = 0;
    for (Object value : objectList) {
        ResultPath newPath = validationEnvironment.getValidatedPath().segment(ix);
        ValidationEnvironment newValidationEnvironment = validationEnvironment.transform(builder -> builder.validatedPath(newPath).validatedValue(value).validatedType(listItemType).directives(directives));
        List<GraphQLError> ruleErrors = runValidationImpl(rule, newValidationEnvironment, listItemType, value);
        errors.addAll(ruleErrors);
        ix++;
    }
    return errors;
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) ArrayList(java.util.ArrayList) ResultPath(graphql.execution.ResultPath) GraphQLError(graphql.GraphQLError) GraphQLDirective(graphql.schema.GraphQLDirective)

Example 3 with GraphQLDirectiveContainer

use of graphql.schema.GraphQLDirectiveContainer in project graphql-java by graphql-java.

the class AppliedDirectivesAreValid method visitGraphQLType.

@Override
protected TraversalControl visitGraphQLType(GraphQLSchemaElement node, TraverserContext<GraphQLSchemaElement> context) {
    SchemaValidationErrorCollector collector = context.getVarFromParents(SchemaValidationErrorCollector.class);
    GraphQLSchema schema = context.getVarFromParents(GraphQLSchema.class);
    if (node instanceof GraphQLDirectiveContainer) {
        GraphQLDirectiveContainer directiveContainer = (GraphQLDirectiveContainer) node;
        for (Map.Entry<String, List<GraphQLDirective>> entry : directiveContainer.getAllDirectivesByName().entrySet()) {
            String directiveName = entry.getKey();
            GraphQLDirective directiveDef = schema.getDirective(directiveName);
            if (directiveDef != null) {
                checkNonRepeatable(collector, directiveContainer, directiveDef, entry.getValue());
            } else {
                addError(collector, format("A definition for directive '%s' could not be found", directiveName));
            }
        }
    }
    return TraversalControl.CONTINUE;
}
Also used : GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) List(java.util.List) GraphQLDirective(graphql.schema.GraphQLDirective) GraphQLSchema(graphql.schema.GraphQLSchema) Map(java.util.Map)

Example 4 with GraphQLDirectiveContainer

use of graphql.schema.GraphQLDirectiveContainer in project graphql-java-extended-validation by graphql-java.

the class DirectivesAndTypeWalker method walkInputType.

private boolean walkInputType(GraphQLInputType inputType, List<GraphQLDirective> directives, BiFunction<GraphQLInputType, GraphQLDirective, Boolean> isSuitable) {
    String typeName = GraphQLTypeUtil.unwrapAll(inputType).getName();
    GraphQLInputType unwrappedInputType = Util.unwrapNonNull(inputType);
    for (GraphQLDirective directive : directives) {
        if (isSuitable.apply(unwrappedInputType, directive)) {
            return seen(typeName, true);
        }
    }
    if (unwrappedInputType instanceof GraphQLInputObjectType) {
        GraphQLInputObjectType inputObjType = (GraphQLInputObjectType) unwrappedInputType;
        if (seenTypes.containsKey(typeName)) {
            return seenTypes.get(typeName);
        }
        seen(typeName, false);
        for (GraphQLInputObjectField inputField : inputObjType.getFieldDefinitions()) {
            inputType = inputField.getType();
            directives = inputField.getDirectives();
            if (walkInputType(inputType, directives, isSuitable)) {
                return seen(typeName, true);
            }
        }
    }
    if (unwrappedInputType instanceof GraphQLList) {
        GraphQLInputType innerListType = Util.unwrapOneAndAllNonNull(unwrappedInputType);
        if (innerListType instanceof GraphQLDirectiveContainer) {
            directives = ((GraphQLDirectiveContainer) innerListType).getDirectives();
            if (walkInputType(innerListType, directives, isSuitable)) {
                return seen(typeName, true);
            }
        }
    }
    return seen(typeName, false);
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLList(graphql.schema.GraphQLList) GraphQLDirectiveContainer(graphql.schema.GraphQLDirectiveContainer) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLDirective(graphql.schema.GraphQLDirective)

Aggregations

GraphQLDirective (graphql.schema.GraphQLDirective)4 GraphQLDirectiveContainer (graphql.schema.GraphQLDirectiveContainer)4 GraphQLInputType (graphql.schema.GraphQLInputType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 List (java.util.List)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Assert.assertNotNull (graphql.Assert.assertNotNull)1 Assert.assertShouldNeverHappen (graphql.Assert.assertShouldNeverHappen)1 DirectivesUtil (graphql.DirectivesUtil)1 GraphQLError (graphql.GraphQLError)1 PublicApi (graphql.PublicApi)1 PublicSpi (graphql.PublicSpi)1 GraphQLString (graphql.Scalars.GraphQLString)1 ResultPath (graphql.execution.ResultPath)1 ValuesResolver (graphql.execution.ValuesResolver)1 Introspection.__EnumValue (graphql.introspection.Introspection.__EnumValue)1 Introspection.__Field (graphql.introspection.Introspection.__Field)1 Introspection.__InputValue (graphql.introspection.Introspection.__InputValue)1 Introspection.__Schema (graphql.introspection.Introspection.__Schema)1 Introspection.__Type (graphql.introspection.Introspection.__Type)1