Angular, graphql apollo error - how to define a custom merge function?

14 Views Asked by At

I have angular apollo and i have predefined some values in apolloCache :

function mergeFields(existing: any, incoming: any) {
  return {...existing, ...incoming};
}

function relaceFields(existing: any, incoming: any) {
  return incoming;
}

  apolloCache = new InMemoryCache({
    possibleTypes: possibleTypesResult.possibleTypes,
    typePolicies: {
        Query: {
            fields: {
                eligibleShippingMethods: {
                    merge: relaceFields,
                },
            },
        },
        Product: {
            fields: {
                customFields: {
                    merge: mergeFields,
                },
                
            },
        },
        Collection: {
            fields: {
                customFields: {
                    merge: mergeFields,
                },
            },
        },
        Order: {
            fields: {
                lines: {
                    merge: relaceFields,
                },
                shippingLines: {
                    merge: relaceFields,
                },
                discounts: {
                    merge: relaceFields,
                },
                shippingAddress: {
                    merge: relaceFields,
                },
                billingAddress: {
                    merge: relaceFields,
                },
            },
        },
        Customer: {
            fields: {
                addresses: {
                    merge: relaceFields,
                },
                customFields: {
                    merge: mergeFields,
                },
            },
        },
    },
});

But now i am getting the error :

Cache data may be lost when replacing the values field of a ManagedFilter object.

This could cause additional (usually avoidable) network requests to fetch data that were otherwise cached.

To address this problem (which is not a bug in Apollo Client), define a custom merge function for the ManagedFilter.values field, so InMemoryCache can safely merge these objects:

existing: [ { "__ref": "ManagedFilterValue:276" }, { "__ref": "ManagedFilterValue:260" }, { "__ref": "ManagedFilterValue:247" }, { "__ref": "ManagedFilterValue:254" }, { "__ref": "ManagedFilterValue:253" }, { "__ref": "ManagedFilterValue:230" } ] incoming: [ { "__ref": "ManagedFilterValue:260" } ]

For more information about these options, please refer to the documentation:

I am calling this :

Query.search(
input: SearchInput!
): SearchResponse!

Which response is :

type SearchResponse {
items: [SearchResult!]!
totalItems: Int!
facetValues: [FacetValueResult!]!
collections: [CollectionResult!]!
topFilters: [ManagedFilter!]!
leftFilters: [ManagedFilter!]!
}

As you can see there is topFilters and leftFilters ManagedFilter types :

type ManagedFilter {
id: ID!
languageCode: LanguageCode!
name: String!
code: String!
values: [ManagedFilterValue!]!
}

and the values type :

type ManagedFilterValue {
id: ID!
languageCode: LanguageCode!
filterId: ID!
name: String!
code: String!
count: Int
}

How can i add ManagedFilter or ManagedFilterValue to the apolloCache typePolicies so i would not be getting this error message from appollo client ?

0

There are 0 best solutions below