0

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?

Steve
  • 4,457
  • 12
  • 48
  • 89
  • 1
    Outside of wrapping the `User.adress` with `Optional`, or introducing a new `UserInput` class (which would be my preferred approach), I can't come up with anything... But I'll give it a think, as I can imagine this is a fairly common scenario. There may be something that the lib could do to make it easier. What did you end up doing, btw? – kaqqao Dec 24 '22 at 20:46
  • 2 things. First is, whenever the UI needs to update everything it posts to every field. Second, avoid using graphql with languages that do not have separate NULL and NONE values (ie stick to python or javascript). – Steve Jan 04 '23 at 16:55
  • I don't know why I didn't remember this option sooner: https://github.com/leangen/graphql-spqr/issues/407#issuecomment-903319005 I'll post it as an answer with your example. – kaqqao Apr 10 '23 at 23:32

1 Answers1

0

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.

kaqqao
  • 12,984
  • 10
  • 64
  • 118