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();
}
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]);
}
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();
}
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);
}
}
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);
}
}
Aggregations