Apollo Cache ignoring Field Alias as key (MockedProvider)

530 Views Asked by At

I am seeing some differences in behaviour between ApolloProvider and MockedProvider and it's throwing an error in testing.

Assuming I have the following query:

query {
  Author {
    id: authorID
    name
  }
}

In ApolloProvider this query creates entries in the Apollo Cache using the field alias as the key, each Author in the cache has an id. Therefore, Apollo can automatically merge entities.

When using MockedProvider, this is not the case. When I mock the following response:

const mockResponse = {
  data: {
    Author: {
      id: 'test!!',
      name: 'test'
    },
  },
}

I get the following error:

console.warn
      Cache data may be lost when replacing the Author field of a Query object.
      
      To address this problem (which is not a bug in Apollo Client), define a custom merge function for the Query.Author field, so InMemoryCache can safely merge these objects:
      
        existing: {"authorID":"test!!"...

So the exact same query in ApolloProvider uses id (field alias) as the key and in MockedProvider it just adds authorID as another field entry. It ignores the field alias and has no key.

Obviously now nothing is able to merge. My first guess is that it's because the MockedProvider does not have access to the schema so it doesn't know that authorID is of type ID? Or am I way off?

One thing that's really weird to me is that my mockResponse doesn't even provide an authorID. My mockResponse is { id: "test!!" } but the cache shows an entry for {"authorID":"test!!"}, so it's somehow 'unaliased' itself.

I'm really struggling to understand what is happening here. Any insight at all would be enormously useful.

0

There are 0 best solutions below