且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

突变后如何更新阿波罗缓存(使用过滤器查询)

更新时间:2022-12-10 09:28:17

当你使用 readQuery 或 writeQuery 时,你应该使用相同的变量名.所以替换

When you use readQuery or writeQuery, you should use the same variable name. So replace

  variables: { id: ownerId }

  variables: { ownerId }

此外,您收到异常的原因是如果数据不在存储中,则 readQuery 会引发异常.这发生在您第一次使用 writeQuery(或使用其他查询获取数据)之前.

Also, the reason you are getting an exception is that readQuery throws an exception if the data is not in the store. That happens before the first time you use writeQuery (or get the data with some other query).

您可以在调用此更改之前将一些默认值写入存储.

You could write some default values to the store before calling this mutation.

您还可以使用返回 null 而不是抛出异常的 readFragment.但这需要对您的代码进行更多更改.

You could also use readFragment that returns null instead of throwing an exception. But that would require more changes to your code.