且构网

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

如何与另一个查询共享缓存的 apollo 客户端数据

更新时间:2022-05-08 22:35:27

GraphQL 客户端无法对解析器的实现做出假设,即使对于人类来说 API 似乎遵循一个简单的模式并且查询的行为似乎微不足道.你需要帮助一下 缓存重定向.

The GraphQL client cannot make assumptions about the implementations of the resolvers even when for a human the API seem to follow an easy pattern and the behaviour of the queries seems trivial. You need to help out a bit with a Cache Redirect.

好的,我在这里为你试过了.我并不真正使用这些 Relay API,但它的工作方式应该与示例非常相似.

Okay, I tried this here for you. I don't really work with these Relay APIs but it should be working quite similarly to the example.

  cacheRedirects: {
    Query: {
      getProduct: (_, args, { getCacheKey }) => ({
        __typename: 'Node',
        node: getCacheKey({ __typename: 'Product', id: args.id }),
      }),
    },
  },