import { ApolloClient , HttpLink , InMemoryCache } from '@apollo/client' ; import { concatPagination } from '@apollo/client/utilities' ; import merge from 'deepmerge' ; import isEqual from 'lodash/isEqual' ; import { useMemo } from 'react' ; export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__' ; let apolloClient ; function createApolloClient ( ) { return new ApolloClient ( { ssrMode : typeof window === 'undefined' , link : new HttpLink ( { uri : 'https://api.thegraph.com/subgraphs/name/graphprotocol/compound-v2' , credentials : 'same-origin' , } ) , cache : new InMemoryCache ( { typePolicies : { Query : { fields : { allPosts : concatPagination ( ) , } , } , } , } ) , } ) ; } export function initializeApollo ( initialState = null ) { const _apolloClient = apolloClient ?? createApolloClient ( ) ; if ( initialState ) { const existingCache = _apolloClient . extract ( ) ; const data = merge ( initialState , existingCache , { arrayMerge : ( destinationArray , sourceArray ) => [ ... sourceArray , ... destinationArray . filter ( ( d ) => sourceArray . every ( ( s ) => ! isEqual ( d , s ) ) ) , ] , } ) ; _apolloClient . cache . restore ( data ) ; } if ( typeof window === 'undefined' ) return _apolloClient ; if ( ! apolloClient ) apolloClient = _apolloClient ; return _apolloClient ; } export function addApolloState ( client , pageProps ) { if ( pageProps ?. props ) { pageProps . props [ APOLLO_STATE_PROP_NAME ] = client . cache . extract ( ) ; } return pageProps ; } export function useApollo ( pageProps ) { const state = pageProps [ APOLLO_STATE_PROP_NAME ] ; const store = useMemo ( ( ) => initializeApollo ( state ) , [ state ] ) ; return store ; }