graphql multiple mutations using prior mutation return results?
Asked Answered
A

2

9

I understand that mutations are sequential, so it makes sense to me that if Mutation 1 creates an entity and returns an id, that Mutation 2 should have access to that id. However I don't see any examples online and can't seem to get it to work. I see that people say you need to handle this in the resolve function of your route but it seems like extra unnecessary code if I can get this in just the query.

For example I have the following where accounts belong to clients and hence need the clientId before being created. However this does not work...

mutation createClientAndAccount($account: AccountInput, $client: ClientInput){
      createClient(client: $client){ clientId }
      createAccount(account: $account, clientId: USE_CLIENT_ID_FROM_ABOVE) { ... } 
    }

I've also tried nesting mutations but didn't have much luck there either...

Is what i'm trying to do possible? Would the resolve function of createAccount have the return data from createClient?

Applied answered 8/6, 2017 at 16:54 Comment(1)
Possible duplicate of GraphQL - Is it possible to set a variable with a result for a mutationMiry
G
3

This is not possible right now, though would be useful.
See this PR.

Goldeye answered 9/6, 2017 at 8:12 Comment(1)
Couple years down the line, and here is a discussion in official GraphQL repo: github.com/graphql/graphql-spec/issues/583 👻Postern
S
0

Maybe using a custom schema directive we could achieve that.

Schema stitching will be a better approach(though usually it is preferred in API Gateway for merging APIs from different services).

If this requirement is very rare in your application, simply creating a new API that can do both CreateClientAndAccount is enough.

Septi answered 9/1, 2022 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.