-
Notifications
You must be signed in to change notification settings - Fork 6
/
urql.ts
49 lines (42 loc) · 1.37 KB
/
urql.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {
cacheExchange,
fetchExchange,
} from 'urql';
import customScalarsExchange from '@atmina/urql-custom-scalars-exchange';
import { createClient } from '@urql/core';
import { SSRExchange } from '@urql/next';
import { registerUrql } from '@urql/next/rsc';
import schema from '../graphql/introspection.json';
import { getBasePath } from './utils';
export const graphqlEndpoint = `${getBasePath()}/api/graphql`;
const scalarsExchange = customScalarsExchange({
// Types don't match for some reason.
// Related:
// - https://github.com/apollographql/apollo-tooling/issues/1491
// - https://spectrum.chat/urql/help/schema-property-kind-is-missing-in-type~29c8f416-068c-485a-adf1-935686b99d05
schema: schema as any,
scalars: {
/* not compatible with next.js serialization limitations, unfortunately */
// Date(value: number) {
// return new Date(value * 1000);
// },
},
});
export function getUrqlClientOptions(ssr: SSRExchange | undefined) {
return {
url: graphqlEndpoint,
exchanges: [
scalarsExchange,
cacheExchange,
...(ssr ? [ssr] : []),
fetchExchange,
],
};
}
export function getUrqlRscClient() {
// this is overly complicated, we could just call `React.cache` here as `registerUrql` does
const { getClient } = registerUrql(() =>
createClient(getUrqlClientOptions(undefined))
);
return getClient();
}