Apollo 客户端中`writeQuery` 和`writeData` 的区别?
Difference between `writeQuery` and `writeData` in Apollo client?
根据关于本地状态管理的 docs,可以使用 writeData
和 writeQuery
将数据附加到缓存。
此处的最佳做法是什么?什么时候一个胜过另一个?
To write the data to the cache, you can use either cache.writeQuery
or cache.writeData
. The only difference between the two is that cache.writeQuery
requires that you pass in a query to validate that the shape of the data you're writing to the cache is the same as the shape of the data required by the query. Under the hood, cache.writeData
automatically constructs a query from the data
object you pass in and calls cache.writeQuery
.
唯一的区别是:你构造传递的查询(通过直接调用cache.writeQuery
),还是让Apollo处理它(通过调用cache.writeData
)?
从 GitHub 上 cache.writeData
的 the source 来看,Apollo 似乎会对您提供的数据进行一些分析以确定最佳使用方式,而cache.writeQuery
将直接接受您传递给它的查询并使用它。
所以,总结一下:
cache.writeQuery
- 优点:可能更快,提供数据形状验证
- 缺点:您必须自己提供查询
cache.writeData
- 优点:让 Apollo 处理构建查询
- 缺点:可能较慢,没有数据形状验证
根据关于本地状态管理的 docs,可以使用 writeData
和 writeQuery
将数据附加到缓存。
此处的最佳做法是什么?什么时候一个胜过另一个?
To write the data to the cache, you can use either
cache.writeQuery
orcache.writeData
. The only difference between the two is thatcache.writeQuery
requires that you pass in a query to validate that the shape of the data you're writing to the cache is the same as the shape of the data required by the query. Under the hood,cache.writeData
automatically constructs a query from thedata
object you pass in and callscache.writeQuery
.
唯一的区别是:你构造传递的查询(通过直接调用cache.writeQuery
),还是让Apollo处理它(通过调用cache.writeData
)?
从 GitHub 上 cache.writeData
的 the source 来看,Apollo 似乎会对您提供的数据进行一些分析以确定最佳使用方式,而cache.writeQuery
将直接接受您传递给它的查询并使用它。
所以,总结一下:
cache.writeQuery
- 优点:可能更快,提供数据形状验证
- 缺点:您必须自己提供查询
cache.writeData
- 优点:让 Apollo 处理构建查询
- 缺点:可能较慢,没有数据形状验证