Search code examples
graphqlapollo-clientapollo-boost

GraphQL with apollo-client, Is it possible to have same id's and different typenames


I had a question and can't find it in documentation. For example I have a list

products with ids 1,2,3,4

And I have another list, categories with ids 1,2,3,4.

example query

{
  products {
     id
     name
     categories {
     id
     name
     }
  }
}

We can see that they both have same ids but different typename inside apollo. Will it create any problem while caching the data? As Apollo normalizes our data with the id's, Help would be appreciated.


Solution

  • Apollo normalizes using both the __typename and id (or _id) fields, so having a Product and a Category with the same id will normally not cause any problems.

    The client normally appends the __typename for every selection set in your query -- so you do not have to actually add the __typename field yourself.

    The config object passed to InMemoryCache includes a addTypename property, which defaults to true. If you set this to false, then the __typename field will not be added and you will see issues with the cache in this scenario unless you have universally unique IDs.