-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathUrqlContext.tsx
39 lines (35 loc) · 1.21 KB
/
UrqlContext.tsx
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
import React, { useContext, useMemo } from 'react';
import { createClient, fetchExchange, Provider } from 'urql';
import InvariantViolated from '@/errors/InvariantViolated';
import { apiHost } from 'app/constants/config';
import { useLocale } from 'app/contexts/IntlContext';
import { SessionContext } from 'app/contexts/SessionContext';
import authExchange from 'app/graphql/urql-exchanges/auth';
import cacheExchange from 'app/graphql/urql-exchanges/cache';
import buildAcceptLanguage from 'app/utils/buildAcceptLanguage';
export default function UrqlContext({
children,
}: {
children: React.ReactNode;
}) {
const { locale } = useLocale();
const sessionContext = useContext(SessionContext);
if (!sessionContext) throw new InvariantViolated('SessionContext is missing');
const client = useMemo(
() =>
createClient({
suspense: true,
exchanges: [
cacheExchange(),
authExchange(sessionContext),
fetchExchange,
],
url: `${apiHost}api/graphql`,
fetchOptions: {
headers: { 'Accept-Language': buildAcceptLanguage(locale) },
},
}),
[sessionContext, locale],
);
return <Provider value={client}>{children}</Provider>;
}