Apollo cache propagates between two different queries

Hi,
I have a problem with apollo cache and I have no idea what is the best way to get around this problem.
Lets assume I have 3 queries:
addressQuery, which returns data:

{
  "id":"f_UW6RWi"
  "city":"CITY B"
  "__typename":"Address"
}

cart which returns data:

{
  "id":"123"
  "address":{
    "id":"f_UW6RWi"
    "city":"CITY B"
    "__typename":"Address"
  }
  "__typename":"Cart"
}

and order:

{
  "id":"321"
  "address":{
    "id":"f_UW6RWi"
    "city":"CITY A"
    "__typename":"Address"
  }
  "__typename":"Order"
}

The problem is that when I fetch an order then the order query changes the address ref and this new ref propagates to cart address. I don’t want such a logic because they are two different addresses with different cities.
I can get around this problem by adding fetchPolicy:no-cache to the order request, but I don’t think that’s the best idea.

  1. Get address Query
  2. Get cart Query
  3. Get order Query
  4. Order query updates address cache.
  5. Address and cart returns query with bad address

This happens, because both of those addresses do have the __typename "Address" and the id "f_UW6RWi - that is your server telling Apollo Client that they are the same address. If this is not the case, you could use a typePolicy to override the keyArgs for the "Address" type to something different from id.

But personally, I would go and start researching why the server is returning two different entities with the same id. That is very odd and should probably not happen.

1 Like