Apollo-Client graphql subscriptions from cache similar field policy read for query

Viewed 168

Is it possible with apollo-client to provide first results for a subscription from cache similar to the cache-and-network fetchPolicy of a watchQuery?

For queries client side resolvers are possible with the read function with field policies. I'm looking for similar for subscriptions.

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,
            });
          }
        }
      }
    }
  }
});

I tried the following, but it seems this hook is not called for subscription or am I missing something?

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,
              })
            );
          }
        }
      }
    }
  }
});

Leaving also the following questions open to me:

  • How to resolve multiple results from cache and how to return them?
  • Just return array of toReference or use a pub/sub rx stream?

Thx community, I really appreciate your help!

0 Answers
Related