GraphQL Mutation design guidelines regarding layout and return values #991
-
Sup all, I had a question regarding the design of GraphQL mutations. I have been doing some digging around:
My questions are:
1. Flat mutations vs hierarchical mutationsLike a lot of mutation designs are like:
Like of the links above, only a few organisations went with something like:
Is there a reason why you would prefer a flattened mutation over a nested one? Surely you could do something like this right?
Which would be, for the first 10 concepts, create a thing under them. Is the flattened mutation model easier to process? Or faster? Or is it because mutations are still fairly new? Is it because you have search operations which make nested mutations redundant? Or have a misunderstood the intention of mutations? 2. Mutation schema layoutI have noticed a common pattern mainly spurned by this blog post: https://www.apollographql.com/blog/graphql/basics/designing-graphql-mutations/ That talks about two concepts:
My question is related to the second point, which is: Should you effectively copy your query schema? So let us say we have something like:
Should the I guess if you have nested mutations, like say you wanted to create the ball and then edit some attribute of the ball then you could do something like:
Or is it better to return BallPayload with all of the attributes that hang off it like so:
And then have the mutation inside the BallPayload entity? like a builder?...after thinking about this...perhaps this might be a bad idea since you have to filter out certain parts of the schema out... I feel like I am stepping into some unknown areas here. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
In the GraphQL spec, only selections over the
You can do either, the reason that we tend to use BallPayload rather than Ball is that it allows for expanding the schema later (this can also apply to why connections are often used over lists even when cursor pagination is not necessary) - GraphQL is big on versionless schemas, so leaving yourself space for additions is nice. For example, imagine your business logic changed and now the type BallPayload {
ball: Ball
# space here to expand your API in future, e.g.:
# remainingCredits: Int
}
type Mutation {
createBall(...): BallPayload
} You may be interested in #252 |
Beta Was this translation helpful? Give feedback.
In the GraphQL spec, only selections over the
Mutation
type (assuming you're using the default operation type names) is executed in serial; all other selection sets are executed in parallel. It's unsafe to run mutations in parallel - very likely to lead to race conditions and the like - so mutations should be done only in the root selection set of a mutation operation, at least for now.You can do either, the reason that we tend to use BallPayload rather than Ball is that it allows for expanding the schema later (this can also apply to why connections are often used over lis…