-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
useQuery.ts
110 lines (98 loc) · 2.97 KB
/
useQuery.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { DocumentNode } from 'graphql';
import { useCallback, useMemo } from 'react';
import { pipe, concat, fromValue, switchMap, map, scan } from 'wonka';
import {
CombinedError,
OperationContext,
RequestPolicy,
Operation,
} from '@urql/core';
import { useClient } from '../context';
import { useSource, useBehaviourSubject } from './useSource';
import { useRequest } from './useRequest';
import { initialState } from './constants';
export interface UseQueryArgs<V> {
query: string | DocumentNode;
variables?: V;
requestPolicy?: RequestPolicy;
pollInterval?: number;
context?: Partial<OperationContext>;
pause?: boolean;
}
export interface UseQueryState<T> {
fetching: boolean;
stale: boolean;
data?: T;
error?: CombinedError;
extensions?: Record<string, any>;
operation?: Operation;
}
export type UseQueryResponse<T> = [
UseQueryState<T>,
(opts?: Partial<OperationContext>) => void
];
export function useQuery<T = any, V = object>(
args: UseQueryArgs<V>
): UseQueryResponse<T> {
const client = useClient();
// This creates a request which will keep a stable reference
// if request.key doesn't change
const request = useRequest(args.query, args.variables);
// Create a new query-source from client.executeQuery
const makeQuery$ = useCallback(
(opts?: Partial<OperationContext>) => {
return client.executeQuery(request, {
requestPolicy: args.requestPolicy,
pollInterval: args.pollInterval,
...args.context,
...opts,
});
},
[client, request, args.requestPolicy, args.pollInterval, args.context]
);
const [query$$, update] = useBehaviourSubject(
useMemo(() => (args.pause ? null : makeQuery$()), [args.pause, makeQuery$])
);
const state = useSource(
useMemo(() => {
return pipe(
query$$,
switchMap(query$ => {
if (!query$) return fromValue({ fetching: false, stale: false });
return concat([
// Initially set fetching to true
fromValue({ fetching: true, stale: false }),
pipe(
query$,
map(({ stale, data, error, extensions, operation }) => ({
fetching: false,
stale: !!stale,
data,
error,
operation,
extensions,
}))
),
// When the source proactively closes, fetching is set to false
fromValue({ fetching: false, stale: false }),
]);
}),
// The individual partial results are merged into each previous result
scan(
(result, partial) => ({
...result,
...partial,
}),
initialState
)
);
}, [query$$]),
initialState
);
// This is the imperative execute function passed to the user
const executeQuery = useCallback(
(opts?: Partial<OperationContext>) => update(makeQuery$(opts)),
[update, makeQuery$]
);
return [state, executeQuery];
}