thunderhub/config/client.tsx
Anthony Potdevin 581185e6b0
chore: 🔧 remove client (#111)
* chore: 🔧 remove client

* chore: 🔧 change cookie name

* chore: 🔧 remove auth param

* chore: 🔧 remove auth components

* chore: 🔧 add getaccount query

* fix: 🐛 tests

* chore: 🔧 get account

* chore: 🔧 status check

* chore: 🔧 remove log

* chore: 🔧 update apollo client

* refactor: ♻️ server side props

* chore: 🔧 ssr queries

* chore: 🔧 more ssr queries

* chore: 🔧 type check

* chore: 🔧 increase nodeinfo limit

Co-authored-by: apotdevin <apotdevincab@gmail.com>
2020-08-05 08:37:02 +02:00

68 lines
2.1 KiB
TypeScript

/* eslint @typescript-eslint/no-var-requires: 0 */
import { IncomingMessage, ServerResponse } from 'http';
import { useMemo } from 'react';
import {
ApolloClient,
InMemoryCache,
NormalizedCacheObject,
} from '@apollo/client';
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();
const { apiUrl: uri } = publicRuntimeConfig;
let apolloClient: ReturnType<typeof createApolloClient> | null = null;
function createIsomorphLink(req?: IncomingMessage, res?: ServerResponse) {
if (typeof window === 'undefined') {
const { SchemaLink } = require('@apollo/client/link/schema');
const { schema } = require('server/schema');
const { getContext } = require('server/schema/context');
return new SchemaLink({
schema,
context: req && res ? getContext(req, res) : {},
});
} else {
const { HttpLink } = require('@apollo/client/link/http');
return new HttpLink({
uri,
credentials: 'same-origin',
});
}
}
function createApolloClient(req?: IncomingMessage, res?: ServerResponse) {
return new ApolloClient({
credentials: 'same-origin',
ssrMode: typeof window === 'undefined',
link: createIsomorphLink(req, res),
cache: new InMemoryCache({
possibleTypes: { Transaction: ['InvoiceType', 'PaymentType'] },
}),
});
}
export function initializeApollo(
initialState: NormalizedCacheObject | null = null,
req?: IncomingMessage,
res?: ServerResponse
) {
const _apolloClient = apolloClient ?? createApolloClient(req, res);
// If your page has Next.js data fetching methods that use Apollo Client, the initial state
// get hydrated here
if (initialState) {
_apolloClient.cache.restore(initialState);
}
// For SSG and SSR always create a new Apollo Client
if (typeof window === 'undefined') return _apolloClient;
// Create the Apollo Client once in the client
if (!apolloClient) apolloClient = _apolloClient;
return _apolloClient;
}
export function useApollo(initialState: NormalizedCacheObject | null) {
const store = useMemo(() => initializeApollo(initialState), [initialState]);
return store;
}