feature: Directive that allows transformation of input types like we can on field resolvers
Describe the Feature Request
I would like to be able to apply a @Uppercase directive on a input string like so
directive @upperCase on INPUT_FIELD_DEFINITION
input JoinRoomInput {
roomCode: String @upperCase
}
And then in a class that implements SchemaDirectiveWiring
we should be able to modify the input object so that when it reaches the mutation method, the roomCode string would be all transformed to uppercase.
@DgsMutation public JoinRoom joinRoom(@InputArgument JoinRoomInput joinRoomInput) {
Describe Preferred Solution
Right now we can transform a field in an output type by doing something like this
@Override
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> env )
{
GraphQLFieldDefinition field = env.getElement();
GraphQLFieldsContainer parentType = env.getFieldsContainer();
if (field.getDirective("stringCase") != null) {
GraphQLArgument argument = field.getDirective("stringCase").getArgument("case");
String argumentValue = ((EnumValue)argument.getArgumentValue().getValue()).getName();
// build a data fetcher that transforms the given value to uppercase/lowercase
DataFetcher originalFetcher = env.getCodeRegistry().getDataFetcher(parentType, field);
DataFetcher dataFetcher = DataFetcherFactories.wrapDataFetcher(originalFetcher, ((dataFetchingEnvironment, value) -> {
if (value instanceof String) {
if (argumentValue.equalsIgnoreCase("UPPER")) {
return ((String) value).toUpperCase();
}
if (argumentValue.equalsIgnoreCase("LOWER")) {
return ((String) value).toLowerCase();
}
}
return value;
}));
// now change the field definition to use the new uppercase/lowercase data fetcher
env.getCodeRegistry().dataFetcher(parentType, field, dataFetcher);
}
return field;
}
We should allow an input to be modified as well before it reaches the java mutation method.
This should be possible to do with the existing mechanism. We will add documentation and some examples for it.
Related to DGS Examples Java.
Did anyone post an example of custom directive implementation on the input fields specifically. The issue is closed but I don’t see such example in the official docs. I am having hard time figuring it out. All the examples out there override data fetcher but that won’t be the case with input fields.