MarkusH
MarkusH

Reputation: 31

GraphQL result for another mutation

Is it meanwhile possible to use the result of a GraphQL mutation as a parameter for another mutation?

I would like to create a user and his address together in one call.

First I write the address and then take the address id for the creation of the user. Filling the necessary data via the inputs works perfectly

Is this possible without triggering a new mutation from the client?

const createUser = gql`
  mutation createUser(
    $user: createUserInput!
    $address: createAddressInput!
  ) {
    createAddress(input: $address) {
      address {
        id       
      }
    }
    createUser(input: $user ) {
      user {
        id
        username
      }
    }
  }
`

Upvotes: 3

Views: 2667

Answers (1)

Bergi
Bergi

Reputation: 664287

No, this is not possible. You either have to make two sequential requests from the client, or adapt your server implementation. I recommend the latter.

You should change your schema so that the CreateUserInput type to not only accepts an addressId: ID but alternatively can accept a newAddress: CreateAddressInput object, which it will then use to create a new address before creating a new user.

Upvotes: 3

Related Questions