import React from "react" ; import { ApolloClient , ApolloProvider , InMemoryCache , NormalizedCacheObject , } from "@apollo/client" ; import { useMemo } from "react" ; import { NextApiRequest , NextApiResponse , NextPage } from "next" ; export interface GraphQlContext { req : NextApiRequest ; res : NextApiResponse ; } let apolloClient ; function createIsomorphicLink ( context ) { /**
     * SSG and SSR
     */ const { HttpLink } = require ( "@apollo/client" ) ; /**
     * API
     */ if ( typeof window === "undefined" ) { const headers = context && "headers" in context && context ?. headers ? context . headers : { } ; return new HttpLink ( { uri : process . env . BUNDESTAGIO_SERVER_URL , credentials : "same-origin" , ... headers , } ) ; } /**
     * Client-side
     */ return new HttpLink ( { uri : "/api/graphql" , credentials : "same-origin" , } ) ; } function createApolloClient ( context ? ) { return new ApolloClient ( { /**
         * Enable SSR mode when not running on the client-side
         */ ssrMode : typeof window === "undefined" , link : createIsomorphicLink ( context ) , cache : new InMemoryCache ( ) , } ) ; } export function initializeApollo ( initialState = null , context ? ) { const _apolloClient = apolloClient ?? createApolloClient ( context ) ; if ( initialState ) { _apolloClient . cache . restore ( initialState ) ; } /**
     * SSG and SSR
     * Always create a new Apollo Client
     */ if ( typeof window === "undefined" ) { return _apolloClient ; } apolloClient = apolloClient ?? _apolloClient ; return apolloClient ; } export const getApolloClient = initializeApollo ; export function useApollo ( initialState ) { const apolloStore = useMemo ( ( ) => initializeApollo ( initialState ) , [ initialState ] ) ; return apolloStore ; } export const withApollo = ( Comp ) => ( props ) => { const apolloStore = useApollo ( props . apolloState ) ; return ( < ApolloProvider client = { apolloStore } > < Comp /> < /ApolloProvider>); } ;