import { useMemo } from 'react' ; import { ApolloClient , HttpLink , InMemoryCache } from '@apollo/client' ; import fetch from 'isomorphic-fetch' ; import config from '@config' ; let apolloClient ; function createApolloClient ( ) { return new ApolloClient ( { ssrMode : typeof window === 'undefined' , link : new HttpLink ( { fetch , uri : config . api . uri , credentials : config . api . credentials } ) , cache : new InMemoryCache ( ) } ) ; } export function initializeApollo ( initialState = null ) { const client = apolloClient || createApolloClient ( ) ; if ( initialState ) { client . cache . restore ( initialState ) ; } if ( typeof window === 'undefined' ) { return client ; } if ( ! apolloClient ) { apolloClient = client ; } return client ; } export function useApollo ( initialState ) { const store = useMemo ( ( ) => initializeApollo ( initialState ) , [ initialState ] ) ; return store ; }