Skip to content

Commit

Permalink
Fix bug in with-apollo example leading to cached lists being duplicat…
Browse files Browse the repository at this point in the history
…ed. (vercel#19812)

Closes vercel#19759
  • Loading branch information
felixmeziere authored Dec 9, 2020
1 parent 782537d commit 3410106
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion examples/with-apollo/lib/apolloClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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__'

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions examples/with-apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 3410106

Please sign in to comment.