diff --git a/examples/with-apollo/lib/apolloClient.js b/examples/with-apollo/lib/apolloClient.js index 0c138b9c41abc..6136d4f1c88d8 100644 --- a/examples/with-apollo/lib/apolloClient.js +++ b/examples/with-apollo/lib/apolloClient.js @@ -2,6 +2,7 @@ import { useMemo } from 'react' import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client' import { concatPagination } from '@apollo/client/utilities' import merge from 'deepmerge' +import isEqual from 'lodash/isEqual' export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__' @@ -36,7 +37,15 @@ export function initializeApollo(initialState = null) { const existingCache = _apolloClient.extract() // Merge the existing cache into data passed from getStaticProps/getServerSideProps - const data = merge(initialState, existingCache) + const data = merge(initialState, existingCache, { + // combine arrays using object equality (like in sets) + arrayMerge: (destinationArray, sourceArray) => [ + ...sourceArray, + ...destinationArray.filter((d) => + sourceArray.every((s) => !isEqual(d, s)) + ), + ], + }) // Restore the cache with the merged data _apolloClient.cache.restore(data) diff --git a/examples/with-apollo/package.json b/examples/with-apollo/package.json index 2763c2a30cc5b..77f3b88e17bca 100644 --- a/examples/with-apollo/package.json +++ b/examples/with-apollo/package.json @@ -9,6 +9,7 @@ "dependencies": { "@apollo/client": "3.1.1", "deepmerge": "^4.2.2", + "lodash": "4.17.20", "graphql": "^15.3.0", "next": "latest", "prop-types": "^15.6.2",