Skip to content

Commit 3e582d3

Browse files
x-jiAkryum
authored andcommitted
docs: Do not modify the object returned from readQuery directly (#1193)
1 parent aaa3573 commit 3e582d3

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

packages/docs/src/fr/guide/apollo/mutations.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ methods: {
3535
// puis avec le résultat de la mutation
3636
update: (store, { data: { addTag } }) => {
3737
// Lecture de la donnée depuis le cache pour cette requête
38-
const data = store.readQuery({ query: TAGS_QUERY })
38+
const { tags } = store.readQuery({ query: TAGS_QUERY })
3939
// Ajout du libellé de la mutation en fin de tableau
40-
data.tags.push(addTag)
40+
const tagsCopy = tags.slice()
41+
tagsCopy.push(addTag)
4142
// Réécriture en cache
42-
store.writeQuery({ query: TAGS_QUERY, data })
43+
store.writeQuery({ query: TAGS_QUERY, { tags: tagsCopy }})
4344
},
4445
// Interface utilisateur optimiste
4546
// Utilisé comme "fausse" donnée dès qu'une requête est réalisée afin que

packages/docs/src/guide/apollo/mutations.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ methods: {
3535
// and then with the real result of the mutation
3636
update: (store, { data: { addTag } }) => {
3737
// Read the data from our cache for this query.
38-
const data = store.readQuery({ query: TAGS_QUERY })
38+
const { tags } = store.readQuery({ query: TAGS_QUERY })
3939
// Add our tag from the mutation to the end
40-
data.tags.push(addTag)
40+
// We don't want to modify the object returned by readQuery directly:
41+
// https://www.apollographql.com/docs/react/caching/cache-interaction/
42+
const tagsCopy = tags.slice()
43+
tagsCopy.push(addTag)
4144
// Write our data back to the cache.
42-
store.writeQuery({ query: TAGS_QUERY, data })
45+
store.writeQuery({ query: TAGS_QUERY, { tags: tagsCopy }})
4346
},
4447
// Optimistic UI
4548
// Will be treated as a 'fake' result as soon as the request is made

packages/docs/src/zh-cn/guide/apollo/mutations.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ methods: {
3434
// 查询将先通过乐观响应、然后再通过真正的变更结果更新
3535
update: (store, { data: { addTag } }) => {
3636
// 从缓存中读取这个查询的数据
37-
const data = store.readQuery({ query: TAGS_QUERY })
37+
const { tags } = store.readQuery({ query: TAGS_QUERY })
3838
// 将变更中的标签添加到最后
39-
data.tags.push(addTag)
39+
const tagsCopy = tags.slice()
40+
tagsCopy.push(addTag)
4041
// 将数据写回缓存
41-
store.writeQuery({ query: TAGS_QUERY, data })
42+
store.writeQuery({ query: TAGS_QUERY, { tags: tagsCopy }})
4243
},
4344
// 乐观 UI
4445
// 将在请求产生时作为“假”结果,使用户界面能够快速更新
@@ -118,4 +119,4 @@ export const resolvers = {
118119
},
119120
},
120121
}
121-
```
122+
```

0 commit comments

Comments
 (0)