Two queries with the same typeName

Hello community.
I have a problem with my cache policies in my apollo-client config file(I guess).
What I have are two queries which return partials of a large object.

For example I have a query in my graph which returns an object with
field1,field2,field3,field4.

And I have 1 query which returns field1,field2 and another query which returns field3,field4.

{
  "data": {
    "getCurrent": {
     {....field1,field2"},
      "__typename": "Customer"
    }
  }
}
{
  "data": {
    "getCurrent": {
     {....field3,field4"},
      "__typename": "Customer"
    }
  }
}

Since both of those queries have the same typename, they override each other.
Is there a way I can my cache understand those queries return a different object? even though they have the same typename and the same key(getCurrent)

I want a solution, if possible that doesn’t merge those queries, but seperates them.

Hi @liorp, try defining a merge function for the getCurrent field.

new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        getCurrent: {
          merge(existing, incoming) {
            // don't merge fields, just return the most recent data
            return incoming;
          },
        },
      },
    },
  },
});

@JeffAuriemma Thanks for the quick reply.
Why are you putting Query in the typePolicies?
isn’t it supposed to be Customer? since Customer is the typename?

if I return only the incoming data, the old data will be lost and the queries will keep over-writing each other, so according to the docs I need to merge them?

Hi @liorp :wave:

Why are you putting Query in the typePolicies?

This is a reference to the default name of the root query type. See more in the GraphQL spec about the Query naming convention. Since getCurrent is a field on Query we can treat it as such.

isn’t it supposed to be Customer? since Customer is the typename?

Could you clarify what you’re trying to accomplish here? Yes, we can add a type policy for Customer but I interpreted this:

I want a solution, if possible that doesn’t merge those queries, but seperates them.

As wanting getCurrent to retain a reference only to the most recently fetched data. I think I may have assumed too much, apologies for that. Are you looking to populate a list of Customers client side with every getCurrent fetch?

Really appreciate your help.

Maybe I really didn’t explain myself correctly.
What I mean is that I want a seperation per query.

Since query1 and query2 over-ride each other all the time I always need to refetch data and my cache doesn’t behave as I expect it to behave, which causes additional load time.
I was wondering if its possible to keep a getCurrent object per query.
for example Query1.getCurrent and Query2.getCurrent, so they wont over-ride each other and both exists in the same time.(I need that since both objects have different params)

As far as I understand apollo doesnt keep objects per query but per typename, so what happens in my scenario is that the first query gets called, writes a getCurrent obj into the cache and when the user access a page that contains the second query getCurrent gets over-ridden, which causes a refetch when the first query is called and so on…
@JeffAuriemma

@liorp are you including the id in the query for getCurrent? I think at this point having a runnable reproduction would be productive. I got one started here with a currentPerson query but I don’t know if it captures your use case so please do feel free to fork it or post your own.

If you’re querying two different Customers, then they should have different id values which would keep them separate in the cache. If you’re querying the same Customer but the results depend on the variable values, then your arguments should be specified on the individual fields (field1/field2/field3/field4) instead of the top level field (getCurrent).
Or if you’re not specifying any arguments and expecting different results back, that sounds like madness