-
-
Notifications
You must be signed in to change notification settings - Fork 443
/
dedup.ts
54 lines (47 loc) · 1.67 KB
/
dedup.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
import { filter, pipe, tap } from 'wonka';
import { Exchange, Operation, OperationResult } from '../types';
/** Default deduplication exchange.
*
* @remarks
* The `dedupExchange` deduplicates queries and subscriptions that are
* started with identical documents and variables by deduplicating by
* their {@link Operation.key}.
* This can prevent duplicate requests from being sent to your GraphQL API.
*
* Because this is a very safe exchange to add to any GraphQL setup, it’s
* not only the default, but we also recommend you to always keep this
* exchange added and included in your setup.
*
* Hint: In React and Vue, some common usage patterns can trigger duplicate
* operations. For instance, in React a single render will actually
* trigger two phases that execute an {@link Operation}.
*/
export const dedupExchange: Exchange = ({ forward, dispatchDebug }) => {
const inFlightKeys = new Set<number>();
const filterIncomingOperation = (operation: Operation) => {
const { key, kind } = operation;
if (kind === 'teardown' || kind === 'mutation') {
inFlightKeys.delete(key);
return true;
}
const isInFlight = inFlightKeys.has(key);
inFlightKeys.add(key);
if (isInFlight) {
dispatchDebug({
type: 'dedup',
message: 'An operation has been deduped.',
operation,
});
}
return !isInFlight;
};
const afterOperationResult = ({ operation, hasNext }: OperationResult) => {
if (!hasNext) {
inFlightKeys.delete(operation.key);
}
};
return ops$ => {
const forward$ = pipe(ops$, filter(filterIncomingOperation));
return pipe(forward(forward$), tap(afterOperationResult));
};
};