Apollo caching type merge

Hello all,
I have a small question about a problem I have currently.
I have my API that returns different types on the queries. For example, the queries returns Type1, Type2 and Type3 for different pages related with the same Object, however when I perform a mutation/action the API returns an Object with the result.
Currently I am defining apollo cache rules to merge the incoming result with what is currently in cache in order to update view.
The problem comes when I want to use the same mutation in 2 different pages because page one have objects of Type1 and page 2 have objects of Type2 so when the mutation returns the value the object that is returned is Type1 so apollo does not finds it and does not update the cache.

We have different types for the same kind of Object because we are using pagination with DjangoPaginationConnection which needs one type for each paginated query we have.

My questions is if its possible to create a rule or something for the cache to say that Type1 and Type2 are the same so cache can identify that the object changed when the mutation runs and update the objects in cache?
This is a part of the cache rules I have right now:

typePolicies: {

    MySubscriptionType: {

        fields: {

            subscribers: {

                merge: false,

            },

            unsubscribers: {

                merge: false,

            },

        },

    },

    SubscriptionType: {

        fields: {

            subscribers: {

                merge: false,

            },

            unsubscribers: {

                merge: false,

            },

        },

    },

I basically need a way to tell the cache that MySubscriptionType and SubscriptionType are the same and they can be merged.

I was able to find a solution for this. I am using this function that comes from Apollo 2.X and is available in 3.X:
Configuring the Apollo Client cache - Client (React) - Apollo GraphQL Docs

This is my code:

export const cache = new InMemoryCache({
    dataIdFromObject(responseObject) {
        switch (responseObject.__typename) {
            case "MySubscriptionType":
                return `SubscriptionType:${responseObject.id}`;
            case "AllSubscriptionType":
                return `SubscriptionType:${responseObject.id}`;
            default:
                return defaultDataIdFromObject(responseObject);
        }
    },
1 Like