-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
35 lines (32 loc) · 957 Bytes
/
index.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
import { isAsyncIterable, mapAsyncIterator, type Plugin } from "@envelop/core";
interface Options {
keepHasNext: boolean;
}
const relayDeferPlugin = (options?: Partial<Options>): Plugin => {
const merged: Options = Object.assign({ keepHasNext: true }, options);
return {
onExecute() {
return {
onExecuteDone({ result, setResult }) {
if (isAsyncIterable(result)) {
setResult(
mapAsyncIterator(result, (chunk) => {
const { hasNext, ...rest } = chunk;
if (hasNext == null) return chunk;
return {
...rest,
...(merged.keepHasNext ? { hasNext } : {}),
extensions: {
...rest.extensions,
is_final: !hasNext,
},
};
})
);
}
},
};
},
};
};
export default relayDeferPlugin;