Cache returns false-positive hit for queries on same type with different fragments

Hi there! I’m facing a practical issue in the usage of GraphQL + Apollo Client. Let’s assume we have a type Person that is being queried on different pages of an application (simplified example):

fragment BasicUser on User {
  firstname
  lastname
}

query UsersForOverview {
  users {
    ...BasicUser
  }
}

fragment EmailUser on User {
  firstname
  lastname
  email
}

query UsersForAddressBook {
  users {
    ...EmailUser
  }
}

Now, when querying UsersForOverview first, Apollo Clients InMemoryCache is being populated. Cache normalization kicks in so I’ll have a number of cache key entries like e.g. User:1234 and User:2345. Afterwards, when running the UsersForAddressBook query the result will be delivered from the cache. Technically, this seems intended behaviour (given how cache normalization works). From a functional perspective though, I’d clearly expect a cache miss, given that not all requested data is present in the cache.

Two questions arise:

  1. Is my understanding correct that the cache works as intended?
  2. Is there a way to make Apollo Clients cache return a cache miss if not all requested fields are present?

If my understanding is correct and there is no way to make this work, this would IMO make the usage of fragments to only retrieve needed data impossible :/, so therefore I hope I’m just not getting a detail here :).

Any help would be greatly appreciated!

Cheers, Daniel

Hi @danbim :wave: welcome to the forum! The cache will behave differently depending on what fetchPolicy it’s using. More info here:

This is the one part I’d push back on - given the scenario you described, the cache would not be able to satisfy the entire query, so there would need to be a network request or error thrown depending on the fetchPolicy being used.

I’m curious as to whether you’re seeing something different in your application though :eyes: