首页 > 解决方案 > Apollo-Client graphql 订阅来自缓存的类似字段策略读取查询

问题描述

是否可以使用 apollo-client 为来自缓存的订阅提供第一个结果,类似于 watchQuery 的缓存和网络 fetchPolicy?

对于查询,客户端解析器可以使用带有字段策略的读取功能。我正在寻找类似的订阅。

import { ApolloClient, InMemoryCache } from '@apollo/client';

const client = new ApolloClient({
  cache: new InMemoryCache({
    typePolicies: {
      Query: {
        fields: {
          book(_, { args, toReference }) {
            return toReference({
              __typename: 'Book',
              id: args.id,
            });
          }
        }
      }
    }
  }
});

我尝试了以下方法,但似乎没有要求订阅这个钩子,或者我错过了什么?

import { ApolloClient, InMemoryCache } from '@apollo/client';

const client = new ApolloClient({
  cache: new InMemoryCache({
    typePolicies: {
      Subscriptions: { // Is it Subscription or Subscriptions?
        fields: {
          books(_, { args, toReference }) {
            console.log("subscription read not called?", args);
            return args.ids.map(
              id => toReference({
                __typename: 'Book',
                id: id,
              })
            );
          }
        }
      }
    }
  }
});

还留给我以下问题:

Thx 社区,非常感谢您的帮助!

标签: javascriptreactjsgraphqlreact-apolloapollo-client

解决方案


推荐阅读