From 4c4e093a76688bf1292833a11be1f78c5c344a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mime=20=C4=8Cuvalo?= Date: Fri, 3 Nov 2023 23:01:07 +0000 Subject: [PATCH] graphql: isomorphlink --- app/apollo.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/apollo.ts b/app/apollo.ts index 31a1bc9..154b089 100644 --- a/app/apollo.ts +++ b/app/apollo.ts @@ -2,11 +2,14 @@ import { ApolloClient, ApolloLink, HttpLink, InMemoryCache, NormalizedCacheObjec import { clientCache, dataIdFromObject } from 'data/localState'; import { BatchHttpLink } from '@apollo/client/link/batch-http'; +import { SchemaLink } from '@apollo/client/link/schema'; import isEqual from 'lodash/isEqual'; import merge from 'deepmerge'; import { onError } from '@apollo/client/link/error'; +import resolvers from 'data/resolvers'; import schema from 'data/schema'; import { useMemo } from 'react'; +import { makeExecutableSchema } from '@graphql-tools/schema'; export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__'; @@ -42,7 +45,20 @@ function createApolloClient(headers?: { [key: string]: string }) { httpLink, // if test is true, debatch batchHttpLink // otherwise, batch ); - const link = ApolloLink.from([errorLink, splitLink]); + + function createIsomorphLink() { + if (typeof window === 'undefined') { + const executableSchema = makeExecutableSchema({ + typeDefs: schema, + resolvers, + }); + return new SchemaLink({ schema: executableSchema }); + } else { + return ApolloLink.from([errorLink, splitLink]); + } + } + + const link = createIsomorphLink(); return new ApolloClient({ ssrMode: typeof window === 'undefined',