use of graphql.schema.GraphQLAppliedDirective 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;
}
use of graphql.schema.GraphQLAppliedDirective in project graphql-java by graphql-java.
the class SchemaGeneratorAppliedDirectiveHelper method buildAppliedDirectives.
static Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> buildAppliedDirectives(SchemaGeneratorHelper.BuildContext buildCtx, Function<Type<?>, GraphQLInputType> inputTypeFactory, List<Directive> directives, List<Directive> extensionDirectives, Introspection.DirectiveLocation directiveLocation, Set<GraphQLDirective> runtimeDirectives, GraphqlTypeComparatorRegistry comparatorRegistry) {
directives = Optional.ofNullable(directives).orElse(emptyList());
extensionDirectives = Optional.ofNullable(extensionDirectives).orElse(emptyList());
List<GraphQLDirective> output = new ArrayList<>();
List<GraphQLAppliedDirective> outputApplied = new ArrayList<>();
for (Directive directive : directives) {
Pair<GraphQLDirective, GraphQLAppliedDirective> pair = buildAppliedDirective(buildCtx, inputTypeFactory, directive, runtimeDirectives, directiveLocation, comparatorRegistry);
output.add(pair.first);
outputApplied.add(pair.second);
}
for (Directive directive : extensionDirectives) {
Pair<GraphQLDirective, GraphQLAppliedDirective> pair = buildAppliedDirective(buildCtx, inputTypeFactory, directive, runtimeDirectives, directiveLocation, comparatorRegistry);
output.add(pair.first);
outputApplied.add(pair.second);
}
return pair(output, outputApplied);
}
use of graphql.schema.GraphQLAppliedDirective in project graphql-java by graphql-java.
the class SchemaGeneratorAppliedDirectiveHelper method buildAppliedDirective.
// builds directives from a type and its extensions
private static Pair<GraphQLDirective, GraphQLAppliedDirective> buildAppliedDirective(SchemaGeneratorHelper.BuildContext buildCtx, Function<Type<?>, GraphQLInputType> inputTypeFactory, Directive directive, Set<GraphQLDirective> directiveDefinitions, Introspection.DirectiveLocation directiveLocation, GraphqlTypeComparatorRegistry comparatorRegistry) {
GraphQLDirective.Builder builder = GraphQLDirective.newDirective().name(directive.getName()).description(buildDescription(buildCtx, directive, null)).comparatorRegistry(comparatorRegistry).validLocations(directiveLocation);
GraphQLAppliedDirective.Builder builderAppliedDirective = GraphQLAppliedDirective.newDirective().name(directive.getName()).description(buildDescription(buildCtx, directive, null)).comparatorRegistry(comparatorRegistry);
Optional<GraphQLDirective> directiveDefOpt = FpKit.findOne(directiveDefinitions, dd -> dd.getName().equals(directive.getName()));
GraphQLDirective graphQLDirective = directiveDefOpt.orElseGet(() -> {
return buildDirectiveDefinitionFromAst(buildCtx, buildCtx.getTypeRegistry().getDirectiveDefinition(directive.getName()).get(), inputTypeFactory);
});
builder.repeatable(graphQLDirective.isRepeatable());
builder.definition(buildCtx.isCaptureAstDefinitions() ? graphQLDirective.getDefinition() : null);
builderAppliedDirective.definition(buildCtx.isCaptureAstDefinitions() ? directive : null);
List<GraphQLArgument> directiveArguments = new ArrayList<>();
List<GraphQLAppliedDirectiveArgument> appliedArguments = new ArrayList<>();
for (Argument arg : directive.getArguments()) {
directiveArguments.add(buildDirectiveArg(buildCtx, arg, graphQLDirective));
appliedArguments.add(buildAppliedArg(buildCtx, arg, graphQLDirective));
}
directiveArguments = transferMissingArguments(buildCtx, directiveArguments, graphQLDirective);
directiveArguments.forEach(builder::argument);
appliedArguments = transferMissingAppliedArguments(appliedArguments, graphQLDirective);
appliedArguments.forEach(builderAppliedDirective::argument);
return pair(builder.build(), builderAppliedDirective.build());
}
use of graphql.schema.GraphQLAppliedDirective in project graphql-java by graphql-java.
the class SchemaGeneratorHelper method buildObjectType.
GraphQLObjectType buildObjectType(BuildContext buildCtx, ObjectTypeDefinition typeDefinition) {
GraphQLObjectType.Builder builder = GraphQLObjectType.newObject();
builder.definition(buildCtx.isCaptureAstDefinitions() ? typeDefinition : null);
builder.name(typeDefinition.getName());
builder.description(buildDescription(buildCtx, typeDefinition, typeDefinition.getDescription()));
builder.comparatorRegistry(buildCtx.getComparatorRegistry());
List<ObjectTypeExtensionDefinition> extensions = objectTypeExtensions(typeDefinition, buildCtx);
builder.extensionDefinitions(buildCtx.isCaptureAstDefinitions() ? extensions : emptyList());
Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> appliedDirectives = buildAppliedDirectives(buildCtx, inputTypeFactory(buildCtx), typeDefinition.getDirectives(), directivesOf(extensions), OBJECT, buildCtx.getDirectives(), buildCtx.getComparatorRegistry());
buildAppliedDirectives(buildCtx, builder, appliedDirectives);
typeDefinition.getFieldDefinitions().forEach(fieldDef -> {
GraphQLFieldDefinition fieldDefinition = buildField(buildCtx, typeDefinition, fieldDef);
builder.field(fieldDefinition);
});
extensions.forEach(extension -> extension.getFieldDefinitions().forEach(fieldDef -> {
GraphQLFieldDefinition fieldDefinition = buildField(buildCtx, typeDefinition, fieldDef);
if (!builder.hasField(fieldDefinition.getName())) {
builder.field(fieldDefinition);
}
}));
buildObjectTypeInterfaces(buildCtx, typeDefinition, builder, extensions);
return directivesObserve(buildCtx, builder.build());
}
use of graphql.schema.GraphQLAppliedDirective in project graphql-java by graphql-java.
the class SchemaPrinter method directivesString.
private String directivesString(Class<? extends GraphQLSchemaElement> parentType, boolean isDeprecated, List<GraphQLAppliedDirective> directives) {
if (isDeprecated) {
directives = addDeprecatedDirectiveIfNeeded(directives);
}
directives = directives.stream().filter(directive -> options.getIncludeDirective().test(directive.getName()) || isDeprecatedDirective(directive)).filter(options.getIncludeSchemaElement()).collect(toList());
if (directives.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
if (parentType != GraphQLSchemaElement.class) {
sb.append(" ");
}
Comparator<? super GraphQLSchemaElement> comparator = getComparator(parentType, GraphQLAppliedDirective.class);
directives = directives.stream().sorted(comparator).collect(toList());
for (int i = 0; i < directives.size(); i++) {
GraphQLAppliedDirective directive = directives.get(i);
sb.append(directiveString(directive));
if (i < directives.size() - 1) {
sb.append(" ");
}
}
return sb.toString();
}
Aggregations