graphql-java-tools icon indicating copy to clipboard operation
graphql-java-tools copied to clipboard

Get value of input type with custom scalars as Map with already coerced values

Open ir-dfasen opened this issue 5 years ago • 0 comments

We have a mutation with an input type that includes custom scalars. We would like to have our mutation resolver being called with the parsed value for the input type as a Map so we are able to see which properties have been set and which not.

scalar A
scalar B

input MyInput {
    a: A
    b: B
}

type Mutation {
    myMutation(input: MyInput): String
}
public class MyResolver implements GraphQLMutationResolver {
    public String myMutation(LinkedHashMap input) {
        if (input.containsKey("a")) {
            // do something
        }
        if (input.containsKey("b")) {
            // do something
        }
    }
}

However if we implement the mutation resolver like this, the map with the parsed custom scalars is passed on to the ObjectMapper in MethodFieldResolver#createDataFetcher and we loose the parsed values for the custom scalars. We currently solve this by retrieving the map with the parsed custom scalars from the DataFetchingEnvironment like this:

public class MyResolver implements GraphQLMutationResolver {
    public String myMutation(LinkedHashMap input, DataFetchingEnvironment environment) {
        input = environment.getArgument("input");

        if (input.containsKey("a")) {
            // do something
        }
        if (input.containsKey("b")) {
            // do something
        }
    }
}

Could the mutation resolver be called with the parsed custom scalars so we do not need to override the input value with the parsed value in the DataFetchingEnvironment? In a way simular to fix #320 - Add support for lists of Scalar field arguments that are already coerced for issue #198 - Input types with custom scalars in method field resolver.

ir-dfasen avatar Aug 06 '20 10:08 ir-dfasen