import { ApolloClient } from '@apollo/client/core' ; import { InMemoryCache , NormalizedCacheObject } from '@apollo/client/cache' ; import getConfig from 'next/config' ; import { BatchHttpLink } from '@apollo/client/link/batch-http' ; import { Locale } from '@island.is/shared/types' ; import { defaultLanguage } from '@island.is/shared/constants' ; import possibleTypes from './fragmentTypes.json' ; const { publicRuntimeConfig , serverRuntimeConfig } = getConfig ( ) ; const isBrowser = process . browser ; let apolloClient = null ; let currentClientLocale = defaultLanguage ; function create ( initialState ? ) { const { graphqlUrl : graphqlServerUrl , graphqlEndpoint : graphqlServerEndpoint , } = serverRuntimeConfig ; const { graphqlUrl : graphqlClientUrl , graphqlEndpoint : graphqlClientEndpoint , } = publicRuntimeConfig ; const graphqlUrl = graphqlServerUrl || graphqlClientUrl ; const graphqlEndpoint = graphqlServerEndpoint || graphqlClientEndpoint ; const httpLink = new BatchHttpLink ( { uri : `${ graphqlUrl }${ graphqlEndpoint }` } ) ; return new ApolloClient ( { name : 'cms-web-client' , version : '0.1' , connectToDevTools : isBrowser , ssrMode : ! isBrowser , link : httpLink , cache : new InMemoryCache ( possibleTypes ) . restore ( initialState || { } ) , } ) ; } export default function initApollo ( initialState ? , clientLocale ? ) { if ( ! isBrowser ) { return create ( initialState ) ; } if ( ! apolloClient ) { apolloClient = create ( initialState ) ; return apolloClient ; } if ( currentClientLocale !== clientLocale ) { currentClientLocale = clientLocale ; apolloClient = create ( initialState ) ; } return apolloClient ; }