/
context.ts
77 lines (68 loc) · 2.24 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as React from 'react';
import { Client } from '@urql/core';
const OBJ = {};
/** `urql`'s React Context.
*
* @remarks
* The React Context that `urql`’s {@link Client} will be provided with.
* You may use the reexported {@link Provider} to provide a `Client` as well.
*/
export const Context: import('react').Context<Client | object> =
React.createContext(OBJ);
/** Provider for `urql`'s {@link Client} to GraphQL hooks.
*
* @remarks
* `Provider` accepts a {@link Client} and provides it to all GraphQL hooks,
* and {@link useClient}.
*
* You should make sure to create a {@link Client} and provide it with the
* `Provider` to parts of your component tree that use GraphQL hooks.
*
* @example
* ```tsx
* import { Provider } from 'urql';
* // All of `@urql/core` is also re-exported by `urql`:
* import { Client, cacheExchange, fetchExchange } from '@urql/core';
*
* const client = new Client({
* url: 'https://API',
* exchanges: [cacheExchange, fetchExchange],
* });
*
* const App = () => (
* <Provider value={client}>
* <Component />
* </Provider>
* );
* ```
*/
export const Provider: React.Provider<Client | object> = Context.Provider;
/** React Consumer component, providing the {@link Client} provided on a parent component.
* @remarks
* This is an alias for {@link Context.Consumer}.
*/
export const Consumer: React.Consumer<Client | object> = Context.Consumer;
Context.displayName = 'UrqlContext';
/** Hook returning a {@link Client} from {@link Context}.
*
* @remarks
* `useClient` is a convenience hook, which accesses `urql`'s {@link Context}
* and returns the {@link Client} defined on it.
*
* This will be the {@link Client} you passed to a {@link Provider}
* you wrapped your elements containing this hook with.
*
* @throws
* In development, if the component you call `useClient()` in is
* not wrapped in a {@link Provider}, an error is thrown.
*/
export const useClient = (): Client => {
const client = React.useContext(Context);
if (client === OBJ && process.env.NODE_ENV !== 'production') {
const error =
"No client has been specified using urql's Provider. please create a client and add a Provider.";
console.error(error);
throw new Error(error);
}
return client as Client;
};