Files
TheGame/packages/web/graphql/client.ts
Pacien Boisson dfff04ebaa [Quests] Frontend (#437)
* squash frontend changes

* Style quest explorer

* Style quest page

* Dates

* Dates

* Typecheck

* Prettier

* Fix create page layout

* Update only OPEN quests

* Repetition info

* Fix create quest errors

* Quest form Textarea

* Quest form Textarea

* Truncate texts

* Redirect if user not logged in

* Tooltips

* Factorize skills tags

* fix username in completions

* Metafam as default guild on creation

* Layouts

* Remove todo

* cooldown

* Rename to "claim quest"

* squash frontend changes

* Style quest explorer

* Style quest page

* Dates

* Dates

* Typecheck

* Prettier

* Fix create page layout

* Update only OPEN quests

* Repetition info

* Fix create quest errors

* Quest form Textarea

* Quest form Textarea

* Truncate texts

* Redirect if user not logged in

* Tooltips

* Factorize skills tags

* fix username in completions

* Metafam as default guild on creation

* Layouts

* Remove todo

* cooldown

* Rename to "claim quest"

* Move ConfirmModal in ds

* Extract pSeed balance

* Fix "created by me" switch

* Reword complete quest

* Style quest form

* prettier

* lint
2021-04-08 15:32:27 +04:00

74 lines
1.7 KiB
TypeScript

import {
initUrqlClient,
NextComponentType,
withUrqlClient,
WithUrqlProps,
} from 'next-urql';
import React, { createElement } from 'react';
import {
cacheExchange,
Client,
createClient,
dedupExchange,
fetchExchange,
ssrExchange,
} from 'urql';
import { CONFIG } from '../config';
import { getTokenFromStore } from '../lib/auth';
export const client = createClient({
url: CONFIG.graphqlURL,
suspense: false,
});
export const getSsrClient = (): [Client, ReturnType<typeof ssrExchange>] => {
const ssrCache = ssrExchange({ isClient: false });
const ssrClient = initUrqlClient(
{
url: CONFIG.graphqlURL,
exchanges: [dedupExchange, cacheExchange, ssrCache, fetchExchange],
},
false,
);
if (!ssrClient) throw new Error('wtf');
return [ssrClient, ssrCache];
};
// We do this to enable ssr cache on pages that are not directly wrapped in 'withUrqlClient' (but on _app)
// https://github.com/FormidableLabs/urql/issues/1481
const customWithUrqlClient = (
WithUrql: NextComponentType,
): React.FC<WithUrqlProps> => ({ pageProps, urqlState, ...props }) => {
return createElement(WithUrql, {
urqlState: pageProps.urqlState || urqlState,
pageProps,
...props,
});
};
export const wrapUrqlClient = (AppOrPage: React.FC<any>) =>
customWithUrqlClient(
withUrqlClient(
(_ssrExchange, ctx) => ({
url: CONFIG.graphqlURL,
fetchOptions: () => {
const token = ctx
? ctx?.req?.headers?.authorization
: getTokenFromStore();
return {
headers: {
Authorization: token ? `Bearer ${token}` : '',
},
};
},
}),
{
neverSuspend: true,
ssr: false,
},
)(AppOrPage),
);