12

I have a situation where I sometimes want to query a list of many objects with a few fields, and sometimes only one object with many fields. As an example, consider these two queries:

  • A query with a many objects and few fields:

    query {
      object(many=true) {
        id
        name
      }
    }
    
  • A query with a single object and many fields:

    query {
      object(many=false) {
        id
        ... (many other fields)
      }
    }
    

I don't want to ask for more fields than necessary in the first query, because this will result in requests with a lot of data (otherwise I could make the fields identical, e.g. using a fragment, solving my issue).

The issue is: The data stored in the cache is normalised and stored according to the __typename and id. This means queries, or atoms of queries, will overwrite each other if these are the same. Therefore, if the second query happens last, the item in the database has no name field, required by components associated with the first query.

The simplest solution is just to ask for name in the second query as well. The problem is that this seems fragile and error prone; for example one might create a third query and forget to ask for name there.

Another option is to make these have different __typename's, i.e. differentiate them on the backend. But this seems sort of artificial; the queries are in reality dealing with the same kind of object.

Is there a canonical way to deal with this issue?

jorgen
  • 3,425
  • 4
  • 31
  • 53

1 Answers1

1

For anyone ending up here: turns out that in the current version at least (@apollo/client 3.3.6) the data is merged, so this is not an issue.

jorgen
  • 3,425
  • 4
  • 31
  • 53