graphql-spqrgraphql-spqr-spring-boot-starter

Graphql SPQR how to represent undefined fields


So I was reading this https://github.com/leangen/graphql-spqr/issues/197 and we have

    mutation createUser {
  createUser(name: "Test", address: null) {
    ...
  }
}

defined with this code

@GraphQLMutation
public User createUser(String name, Optional<Address> address) { ... }

as the way to say "Create a user with a name of Test and an undefined address" (ie do not overwrite the value of address in the database). Which is backwards, but we can't do anything about that for weird Jackson/Java reasons.

But what if I have

   mutation createUser{
createUser(User: {name: "Test", address: null}){
...
}
}

defined with this code

@GraphQLMutation
public User createUser(User user) { ... }

Where a User object has a Name and an Address.

Is there a way to make this work?


Solution

  • If you deconstruct the input, e.g. instead of

    @GraphQLQuery
    public User createUser(User user) {...}
    

    you have

    @GraphQLQuery
    public User createUser(String name, Optional<Address> address) {...}
    

    and tell SPQR to generate Relay-compliant mutations:

    generator.withRelayCompliantMutations()
    

    or graphql.spqr.relay.enabled=true in application.properties with Spring Starter.

    SPQR will generate a specific input type for each mutation that doesn't already take a single object. So your createUser will still accept a singular input object of the CreateUserInput type.

    That's probably the best option I can come up with.