Commit 0bd6434d authored by Evan Read's avatar Evan Read

Merge branch 'docs-fe-graphql-best-practices' into 'master'

Docs: Add GraphQL Best Practices section within Frontend GraphQL

See merge request gitlab-org/gitlab!65568
parents f024f880 cc39eba9
...@@ -906,6 +906,35 @@ apollo: { ...@@ -906,6 +906,35 @@ apollo: {
}, },
``` ```
### Best Practices
#### When to use (and not use) `update` hook in mutations
Apollo Client's [`.mutate()`](https://www.apollographql.com/docs/react/api/core/ApolloClient/#ApolloClient.mutate)
method exposes an `update` hook that is invoked twice during the mutation lifecycle:
- Once at the beginning. That is, before the mutation has completed.
- Once after the mutation has completed.
You should use this hook only if you're adding or removing an item from the store
(that is, ApolloCache). If you're _updating_ an existing item, it is usually represented by
a global `id`.
In that case, presence of this `id` in your mutation query definition makes the store update
automatically. Here's an example of a typical mutation query with `id` present in it:
```graphql
mutation issueSetWeight($input: IssueSetWeightInput!) {
issuableSetWeight: issueSetWeight(input: $input) {
issuable: issue {
id
weight
}
errors
}
}
```
### Testing ### Testing
#### Generating the GraphQL schema #### Generating the GraphQL schema
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment