Skip to content

Commit

Permalink
docs: updated mutation docs to include instructions and caveats for i… (
Browse files Browse the repository at this point in the history
#3146)

* docs: updated mutation docs to include instructions and caveats for implementing nested mutations

* docs: removed unnecessary RELEASE.md file and added attribution to third party links
  • Loading branch information
bradleyoesch authored Nov 16, 2023
1 parent fb66958 commit a6832f2
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion docs/general/mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type Mutation {

<Note>

Mutations with void-result go against [GQL best practices](https://graphql-rules.com/rules/mutation-payload)
Mutations with void-result go against [this community-created guide on GQL best practices](https://graphql-rules.com/rules/mutation-payload).

</Note>

Expand Down Expand Up @@ -134,3 +134,68 @@ type Mutation {
updateFruitWeight(input: UpdateFruitWeightInput!): Fruit!
}
```

## Nested mutations

To avoid a graph becoming too large and to improve discoverability, it can be helpful to group mutations in a namespace, as described by [Apollo's guide on Namespacing by separation of concerns](https://www.apollographql.com/docs/technotes/TN0012-namespacing-by-separation-of-concern/).

```graphql
type Mutation {
fruit: FruitMutations!
}

type FruitMutations {
add(input: AddFruitInput): Fruit!
updateWeight(input: UpdateFruitWeightInput!): Fruit!
}
```

Since all GraphQL operations are fields, we can define a `FruitMutation` type and add mutation fields to it like we could add mutation fields to the root `Mutation` type.

```python
import strawberry


@strawberry.type
class FruitMutations:
@strawberry.mutation
def add(self, info, input: AddFruitInput) -> Fruit:
# ...

@strawberry.mutation
def update_weight(self, info, input: UpdateFruitWeightInput) -> Fruit:
# ...


@strawberry.type
class Mutation:
fruit: FruitMutations
```

<Note>
Fields on the root `Mutation` type are resolved serially. Namespace types introduce the potential for mutations to be resolved asynchronously and in parallel because the mutation fields that mutate data are no longer at the root level.

To guarantee serial execution when namespace types are used, clients should use aliases to select the root mutation field for each mutation.
In the following example, once `addFruit` execution is complete, `updateFruitWeight` begins.

```graphql
mutation (
$addFruitInput: AddFruitInput!
$updateFruitWeightInput: UpdateFruitWeightInput!
) {
addFruit: fruit {
add(input: $addFruitInput) {
id
}
}

updateFruitWeight: fruit {
updateWeight(input: $updateFruitWeightInput) {
id
}
}
}
```

For more details, see [Apollo's guide on Namespaces for serial mutations](https://www.apollographql.com/docs/technotes/TN0012-namespacing-by-separation-of-concern/#namespaces-for-serial-mutations) and [Rapid API's Interactive Guide to GraphQL Queries: Aliases and Variables](https://rapidapi.com/guides/graphql-aliases-variables).
</Note>

0 comments on commit a6832f2

Please sign in to comment.