Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Directive that allows transformation of input types like we can on field resolvers #1035

Closed
SaranSundar opened this issue May 6, 2022 · 4 comments
Labels
documentation Improvements or additions to documentation good-first-issues

Comments

@SaranSundar
Copy link

SaranSundar commented May 6, 2022

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.

@SaranSundar SaranSundar added the enhancement New feature or request label May 6, 2022
@srinivasankavitha
Copy link
Contributor

This should be possible to do with the existing mechanism. We will add documentation and some examples for it.

@srinivasankavitha srinivasankavitha added documentation Improvements or additions to documentation good-first-issues and removed enhancement New feature or request labels Jun 17, 2022
@berngp
Copy link
Contributor

berngp commented Jun 17, 2022

Related to DGS Examples Java.

@taptew
Copy link

taptew commented Jan 10, 2024

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.

@figuldan
Copy link

Example of an input field implementation is still missing, any progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good-first-issues
Projects
None yet
Development

No branches or pull requests

5 participants