Unverified Commit 8e9e9494 authored by Matthew Lilley's avatar Matthew Lilley Committed by GitHub

Fix issues with apollo cache data merging and restoration (#17681)

This PR fixes issues in two apollo examples where cached queries are lost, and cached data is merged incorrectly.
parent 6ea504a6
import { useMemo } from 'react' import { useMemo } from 'react'
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client' import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client'
import { concatPagination } from '@apollo/client/utilities' import { concatPagination } from '@apollo/client/utilities'
import merge from 'deepmerge'
let apolloClient let apolloClient
...@@ -31,9 +32,12 @@ export function initializeApollo(initialState = null) { ...@@ -31,9 +32,12 @@ export function initializeApollo(initialState = null) {
if (initialState) { if (initialState) {
// Get existing cache, loaded during client side data fetching // Get existing cache, loaded during client side data fetching
const existingCache = _apolloClient.extract() const existingCache = _apolloClient.extract()
// Restore the cache using the data passed from getStaticProps/getServerSideProps
// combined with the existing cached data // Merge the existing cache into data passed from getStaticProps/getServerSideProps
_apolloClient.cache.restore({ ...existingCache, ...initialState }) const data = merge(initialState, existingCache)
// Restore the cache with the merged data
_apolloClient.cache.restore(data)
} }
// For SSG and SSR always create a new Apollo Client // For SSG and SSR always create a new Apollo Client
if (typeof window === 'undefined') return _apolloClient if (typeof window === 'undefined') return _apolloClient
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
}, },
"dependencies": { "dependencies": {
"@apollo/client": "^3.0.0", "@apollo/client": "^3.0.0",
"deepmerge": "^4.2.2",
"graphql": "14.5.8", "graphql": "14.5.8",
"next": "latest", "next": "latest",
"prop-types": "^15.6.0", "prop-types": "^15.6.0",
......
import { useMemo } from 'react' import { useMemo } from 'react'
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client' import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client'
import { concatPagination } from '@apollo/client/utilities' import { concatPagination } from '@apollo/client/utilities'
import merge from 'deepmerge'
let apolloClient let apolloClient
...@@ -31,9 +32,12 @@ export function initializeApollo(initialState = null) { ...@@ -31,9 +32,12 @@ export function initializeApollo(initialState = null) {
if (initialState) { if (initialState) {
// Get existing cache, loaded during client side data fetching // Get existing cache, loaded during client side data fetching
const existingCache = _apolloClient.extract() const existingCache = _apolloClient.extract()
// Restore the cache using the data passed from getStaticProps/getServerSideProps
// combined with the existing cached data // Merge the existing cache into data passed from getStaticProps/getServerSideProps
_apolloClient.cache.restore({ ...existingCache, ...initialState }) const data = merge(initialState, existingCache)
// Restore the cache with the merged data
_apolloClient.cache.restore(data)
} }
// For SSG and SSR always create a new Apollo Client // For SSG and SSR always create a new Apollo Client
if (typeof window === 'undefined') return _apolloClient if (typeof window === 'undefined') return _apolloClient
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
}, },
"dependencies": { "dependencies": {
"@apollo/client": "3.1.1", "@apollo/client": "3.1.1",
"deepmerge": "^4.2.2",
"graphql": "^15.3.0", "graphql": "^15.3.0",
"next": "latest", "next": "latest",
"prop-types": "^15.6.2", "prop-types": "^15.6.2",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment