Apollo Client:更新缓存时是 writeFragment 还是 readFragment?
Apollo Client: writeFragment or readFragment when updating cache?
在useMutation 的update
钩子中,Apollo 的文档推荐使用writeFragment
获取对新添加对象的内部引用。我觉得这很奇怪,因为该对象已经存在于缓存中。所以我用 readFragment
测试了它,果然,它运行良好。在此用例中,是否优先使用 writeFragment
而不是 readFragment
?
示例 1:
https://www.apollographql.com/docs/react/data/mutations/#making-all-other-cache-updates
const [addTodo] = useMutation(ADD_TODO, {
update(cache, { data: { addTodo } }) {
cache.modify({
fields: {
todos(existingTodos = []) {
const newTodoRef = cache.writeFragment({
data: addTodo,
fragment: gql`
fragment NewTodo on Todo {
id
type
}
`
});
return [...existingTodos, newTodoRef];
}
}
});
该页面的摘录:
With the help of cache.writeFragment we get an internal reference to
the added todo, then store that reference in the ROOT_QUERY.todos
array.
示例 2:
const [addComment] = useMutation(ADD_COMMENT, {
update(cache, { data: { addComment } }) {
cache.modify({
fields: {
comments(existingCommentRefs = [], { readField }) {
const newCommentRef = cache.writeFragment({
data: addComment,
fragment: gql`
fragment NewComment on Comment {
id
text
}
`
});
return [...existingCommentRefs, newCommentRef];
}
}
});
}
});
该页面的摘录:
the comment was already added to the cache by useMutation.
Consequently, cache.writeFragment returns a reference to the existing
object.
我也在 Apollo Client 的讨论区 (https://github.com/apollographql/apollo-client/discussions/7515) 上发布了这个问题,但没有得到回复。
从缓存中获取项目时使用 writeFragment
优于 readFragment
的好处在此处解释(摘自 https://www.apollographql.com/docs/react/caching/cache-interaction/#example-updating-the-cache-after-a-mutation):
If you call writeFragment with an options.data object that the cache is able to identify, based on its __typename and primary key fields, you can avoid passing options.id to writeFragment.
Whether you provide options.id explicitly or let writeFragment figure it out using options.data, writeFragment returns a Reference to the identified object.
This behavior makes writeFragment a good tool for obtaining a Reference to an existing object in the cache, which can come in handy
when writing an update function for useMutation
这是违反直觉的,因为名称 writeFragment
暗示它用于写入缓存而不是从中读取,但这似乎是推荐的最佳做法。
在useMutation 的update
钩子中,Apollo 的文档推荐使用writeFragment
获取对新添加对象的内部引用。我觉得这很奇怪,因为该对象已经存在于缓存中。所以我用 readFragment
测试了它,果然,它运行良好。在此用例中,是否优先使用 writeFragment
而不是 readFragment
?
示例 1:
https://www.apollographql.com/docs/react/data/mutations/#making-all-other-cache-updates
const [addTodo] = useMutation(ADD_TODO, {
update(cache, { data: { addTodo } }) {
cache.modify({
fields: {
todos(existingTodos = []) {
const newTodoRef = cache.writeFragment({
data: addTodo,
fragment: gql`
fragment NewTodo on Todo {
id
type
}
`
});
return [...existingTodos, newTodoRef];
}
}
});
该页面的摘录:
With the help of cache.writeFragment we get an internal reference to the added todo, then store that reference in the ROOT_QUERY.todos array.
示例 2:
const [addComment] = useMutation(ADD_COMMENT, {
update(cache, { data: { addComment } }) {
cache.modify({
fields: {
comments(existingCommentRefs = [], { readField }) {
const newCommentRef = cache.writeFragment({
data: addComment,
fragment: gql`
fragment NewComment on Comment {
id
text
}
`
});
return [...existingCommentRefs, newCommentRef];
}
}
});
}
});
该页面的摘录:
the comment was already added to the cache by useMutation. Consequently, cache.writeFragment returns a reference to the existing object.
我也在 Apollo Client 的讨论区 (https://github.com/apollographql/apollo-client/discussions/7515) 上发布了这个问题,但没有得到回复。
从缓存中获取项目时使用 writeFragment
优于 readFragment
的好处在此处解释(摘自 https://www.apollographql.com/docs/react/caching/cache-interaction/#example-updating-the-cache-after-a-mutation):
If you call writeFragment with an options.data object that the cache is able to identify, based on its __typename and primary key fields, you can avoid passing options.id to writeFragment.
Whether you provide options.id explicitly or let writeFragment figure it out using options.data, writeFragment returns a Reference to the identified object.
This behavior makes writeFragment a good tool for obtaining a Reference to an existing object in the cache, which can come in handy when writing an update function for useMutation
这是违反直觉的,因为名称 writeFragment
暗示它用于写入缓存而不是从中读取,但这似乎是推荐的最佳做法。