-
-
Notifications
You must be signed in to change notification settings - Fork 457
/
context.ts
58 lines (53 loc) · 1.85 KB
/
context.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
50
51
52
53
54
55
56
57
58
import { setContext, getContext } from 'svelte';
import type { ClientOptions } from '@urql/core';
import { Client } from '@urql/core';
const _contextKey = '$$_urql';
/** Returns a provided {@link Client}.
*
* @remarks
* `getContextClient` returns the {@link Client} that’s previously
* been provided on Svelte’s context with {@link setContextClient}.
*
* This is useful to create a `Client` on Svelte’s context once, and
* then pass it to all GraphQL store functions without importing it
* from a singleton export.
*
* @throws
* In development, if `getContextClient` can’t get a {@link Client}
* from Svelte’s context, an error will be thrown.
*/
export const getContextClient = (): Client => {
const client = getContext(_contextKey);
if (process.env.NODE_ENV !== 'production' && !client) {
throw new Error(
'No urql Client was found in Svelte context. Did you forget to call setContextClient?'
);
}
return client as Client;
};
/** Provides a {@link Client} to a component’s children.
*
* @remarks
* `setContextClient` updates the Svelte context to provide
* a {@link Client} to be later retrieved using the
* {@link getContextClient} function.
*/
export const setContextClient = (client: Client): void => {
setContext(_contextKey, client);
};
/** Creates a {@link Client} and provides it to a component’s children.
*
* @param args - a {@link ClientOptions} object to create a `Client` with.
* @returns the created {@link Client}.
*
* @remarks
* `initContextClient` is a convenience wrapper around
* `setContextClient` that accepts {@link ClientOptions},
* creates a {@link Client} and provides it to be later
* retrieved using the {@link getContextClient} function.
*/
export const initContextClient = (args: ClientOptions): Client => {
const client = new Client(args);
setContextClient(client);
return client;
};