Search in sources :

Example 1 with GraphQLDirective

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

the class SchemaGenerator method buildField.

private GraphQLFieldDefinition buildField(BuildContext buildCtx, TypeDefinition parentType, FieldDefinition fieldDef) {
    GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition.newFieldDefinition();
    builder.definition(fieldDef);
    builder.name(fieldDef.getName());
    builder.description(schemaGeneratorHelper.buildDescription(fieldDef, fieldDef.getDescription()));
    builder.deprecate(schemaGeneratorHelper.buildDeprecationReason(fieldDef.getDirectives()));
    GraphQLDirective[] directives = buildDirectives(fieldDef.getDirectives(), Collections.emptyList(), Introspection.DirectiveLocation.FIELD);
    builder.withDirectives(directives);
    fieldDef.getInputValueDefinitions().forEach(inputValueDefinition -> builder.argument(buildArgument(buildCtx, inputValueDefinition)));
    GraphQLOutputType fieldType = buildOutputType(buildCtx, fieldDef.getType());
    builder.type(fieldType);
    builder.dataFetcherFactory(buildDataFetcherFactory(buildCtx, parentType, fieldDef, fieldType, Arrays.asList(directives)));
    return builder.build();
}
Also used : GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLDirective(graphql.schema.GraphQLDirective)

Example 2 with GraphQLDirective

use of graphql.schema.GraphQLDirective 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 3 with GraphQLDirective

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

the class SchemaGeneratorHelper method buildDirective.

// builds directives from a type and its extensions
public GraphQLDirective buildDirective(Directive directive, Introspection.DirectiveLocation directiveLocation) {
    List<GraphQLArgument> arguments = directive.getArguments().stream().map(this::buildDirectiveArgument).collect(Collectors.toList());
    GraphQLDirective.Builder builder = GraphQLDirective.newDirective().name(directive.getName()).description(buildDescription(directive, null)).validLocations(directiveLocation);
    for (GraphQLArgument argument : arguments) {
        builder.argument(argument);
    }
    return builder.build();
}
Also used : GraphQLArgument(graphql.schema.GraphQLArgument) GraphQLDirective(graphql.schema.GraphQLDirective)

Example 4 with GraphQLDirective

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

the class KnownArgumentNames method checkArgument.

@Override
public void checkArgument(Argument argument) {
    GraphQLDirective directiveDef = getValidationContext().getDirective();
    if (directiveDef != null) {
        GraphQLArgument directiveArgument = directiveDef.getArgument(argument.getName());
        if (directiveArgument == null) {
            String message = String.format("Unknown directive argument %s", argument.getName());
            addError(ValidationErrorType.UnknownDirective, argument.getSourceLocation(), message);
        }
        return;
    }
    GraphQLFieldDefinition fieldDef = getValidationContext().getFieldDef();
    if (fieldDef == null)
        return;
    GraphQLArgument fieldArgument = fieldDef.getArgument(argument.getName());
    if (fieldArgument == null) {
        String message = String.format("Unknown field argument %s", argument.getName());
        addError(ValidationErrorType.UnknownArgument, argument.getSourceLocation(), message);
    }
}
Also used : GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) GraphQLDirective(graphql.schema.GraphQLDirective)

Example 5 with GraphQLDirective

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

the class KnownDirectives method checkDirective.

@Override
public void checkDirective(Directive directive, List<Node> ancestors) {
    GraphQLDirective graphQLDirective = getValidationContext().getSchema().getDirective(directive.getName());
    if (graphQLDirective == null) {
        String message = String.format("Unknown directive %s", directive.getName());
        addError(ValidationErrorType.UnknownDirective, directive.getSourceLocation(), message);
        return;
    }
    Node ancestor = ancestors.get(ancestors.size() - 1);
    if (hasInvalidLocation(graphQLDirective, ancestor)) {
        String message = String.format("Directive %s not allowed here", directive.getName());
        addError(ValidationErrorType.MisplacedDirective, directive.getSourceLocation(), message);
    }
}
Also used : Node(graphql.language.Node) GraphQLDirective(graphql.schema.GraphQLDirective)

Aggregations

GraphQLDirective (graphql.schema.GraphQLDirective)8 GraphQLArgument (graphql.schema.GraphQLArgument)4 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)2 Argument (graphql.language.Argument)1 Directive (graphql.language.Directive)1 Node (graphql.language.Node)1 GraphQLNonNull (graphql.schema.GraphQLNonNull)1 GraphQLOutputType (graphql.schema.GraphQLOutputType)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1