apollo-client: typePolicies exisitngData arg is incorrect or missused

Based on the example in point 3 here, to replace the cache redirect in apollo-client v3 we need go from this

const cache = new InMemoryCache({
  cacheRedirects: {
    Query: {
      book: (_, args, { getCacheKey }) =>
        getCacheKey({ __typename: 'Book', id: args.id })
    },
  },
});

to this

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

But I’m not sure that’s correct.

If I load a list of books, and then I go to book with id 1, the first time existingData will be undefined. Now if I try to load book with id 2, existing data has {__ref: "books:1"} not undefined, so it will load the incorrect book.

What is the purpose of the existingData arg?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Glad to hear it’s working @tafelito! This is great feedback - I think we should consider changing @apollo/client to be a peer dep in the @apollo/link-X packages, instead of a dep. I’ve made a note - thanks!