Directives
See Schema Directives for a detailed explanation about directives including examples on how to define them in the SDL and to create the required classes.
To add your custom SchemaDirectiveWiring
to graphql-spring-boot
create a bean
of type SchemaDirective
to have it automatically passed along to the SchemaParser
SchemaDirective.create("uppercase", new UppercaseDirective())
Basic usage
Let’s say you defined a custom directive to make text uppercase in a resource schema.graphqls
:
directive @uppercase on FIELD_DEFINITION
type Query {
hello: String @uppercase
}
And the actual implementation is the following:
public class UppercaseDirective implements SchemaDirectiveWiring {
@Override
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> env) {
GraphQLFieldDefinition field = env.getElement();
DataFetcher dataFetcher = DataFetcherFactories.wrapDataFetcher(field.getDataFetcher(), {
dataFetchingEnvironment, value ->
if (value == null) {
return null
}
return ((String) value).toUpperCase()
})
return field.transform({ builder -> builder.dataFetcher(dataFetcher) });
}
}
Add our custom directive as a bean:
@Bean
public SchemaDirective myCustomDirective() {
return new SchemaDirective("uppercase", new UppercaseDirective());
}
It will then be automatically picked up and added to the SchemaParser
.
Supported locations
Support for directives is currently limited to the following locations:
OBJECT
FIELD_DEFINITION
ARGUMENT_DEFINITION
INTERFACE
UNION
ENUM
INPUT_OBJECT
INPUT_FIELD_DEFINITION
Meaning directives for the following locations are currently not yet supported:
SCALAR
ENUM_VALUE