|
1 | 1 | import { invariant } from '../jsutils/invariant.js'; |
2 | 2 | import { isPromise } from '../jsutils/isPromise.js'; |
3 | 3 | import type { ObjMap } from '../jsutils/ObjMap.js'; |
4 | | -import type { Path } from '../jsutils/Path.js'; |
5 | 4 | import { pathToArray } from '../jsutils/Path.js'; |
6 | | -import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js'; |
7 | 5 | import { promiseWithResolvers } from '../jsutils/promiseWithResolvers.js'; |
8 | 6 |
|
9 | | -import type { |
10 | | - GraphQLError, |
11 | | - GraphQLFormattedError, |
12 | | -} from '../error/GraphQLError.js'; |
13 | | - |
14 | | -/** |
15 | | - * The result of GraphQL execution. |
16 | | - * |
17 | | - * - `errors` is included when any errors occurred as a non-empty array. |
18 | | - * - `data` is the result of a successful execution of the query. |
19 | | - * - `hasNext` is true if a future payload is expected. |
20 | | - * - `extensions` is reserved for adding non-standard properties. |
21 | | - * - `incremental` is a list of the results from defer/stream directives. |
22 | | - */ |
23 | | -export interface ExecutionResult< |
24 | | - TData = ObjMap<unknown>, |
25 | | - TExtensions = ObjMap<unknown>, |
26 | | -> { |
27 | | - errors?: ReadonlyArray<GraphQLError>; |
28 | | - data?: TData | null; |
29 | | - extensions?: TExtensions; |
30 | | -} |
31 | | - |
32 | | -export interface FormattedExecutionResult< |
33 | | - TData = ObjMap<unknown>, |
34 | | - TExtensions = ObjMap<unknown>, |
35 | | -> { |
36 | | - errors?: ReadonlyArray<GraphQLFormattedError>; |
37 | | - data?: TData | null; |
38 | | - extensions?: TExtensions; |
39 | | -} |
40 | | - |
41 | | -export interface ExperimentalIncrementalExecutionResults< |
42 | | - TData = unknown, |
43 | | - TExtensions = ObjMap<unknown>, |
44 | | -> { |
45 | | - initialResult: InitialIncrementalExecutionResult<TData, TExtensions>; |
46 | | - subsequentResults: AsyncGenerator< |
47 | | - SubsequentIncrementalExecutionResult<TData, TExtensions>, |
48 | | - void, |
49 | | - void |
50 | | - >; |
51 | | -} |
52 | | - |
53 | | -export interface InitialIncrementalExecutionResult< |
54 | | - TData = ObjMap<unknown>, |
55 | | - TExtensions = ObjMap<unknown>, |
56 | | -> extends ExecutionResult<TData, TExtensions> { |
57 | | - data: TData; |
58 | | - pending: ReadonlyArray<PendingResult>; |
59 | | - hasNext: true; |
60 | | - extensions?: TExtensions; |
61 | | -} |
62 | | - |
63 | | -export interface FormattedInitialIncrementalExecutionResult< |
64 | | - TData = ObjMap<unknown>, |
65 | | - TExtensions = ObjMap<unknown>, |
66 | | -> extends FormattedExecutionResult<TData, TExtensions> { |
67 | | - data: TData; |
68 | | - pending: ReadonlyArray<PendingResult>; |
69 | | - hasNext: boolean; |
70 | | - extensions?: TExtensions; |
71 | | -} |
72 | | - |
73 | | -export interface SubsequentIncrementalExecutionResult< |
74 | | - TData = unknown, |
75 | | - TExtensions = ObjMap<unknown>, |
76 | | -> { |
77 | | - pending?: ReadonlyArray<PendingResult>; |
78 | | - incremental?: ReadonlyArray<IncrementalResult<TData, TExtensions>>; |
79 | | - completed?: ReadonlyArray<CompletedResult>; |
80 | | - hasNext: boolean; |
81 | | - extensions?: TExtensions; |
82 | | -} |
83 | | - |
84 | | -export interface FormattedSubsequentIncrementalExecutionResult< |
85 | | - TData = unknown, |
86 | | - TExtensions = ObjMap<unknown>, |
87 | | -> { |
88 | | - hasNext: boolean; |
89 | | - pending?: ReadonlyArray<PendingResult>; |
90 | | - incremental?: ReadonlyArray<FormattedIncrementalResult<TData, TExtensions>>; |
91 | | - completed?: ReadonlyArray<FormattedCompletedResult>; |
92 | | - extensions?: TExtensions; |
93 | | -} |
94 | | - |
95 | | -interface BareDeferredGroupedFieldSetResult<TData = ObjMap<unknown>> { |
96 | | - errors?: ReadonlyArray<GraphQLError>; |
97 | | - data: TData; |
98 | | -} |
99 | | - |
100 | | -export interface IncrementalDeferResult< |
101 | | - TData = ObjMap<unknown>, |
102 | | - TExtensions = ObjMap<unknown>, |
103 | | -> extends BareDeferredGroupedFieldSetResult<TData> { |
104 | | - id: string; |
105 | | - subPath?: ReadonlyArray<string | number>; |
106 | | - extensions?: TExtensions; |
107 | | -} |
| 7 | +import type { GraphQLError } from '../error/GraphQLError.js'; |
108 | 8 |
|
109 | | -export interface FormattedIncrementalDeferResult< |
110 | | - TData = ObjMap<unknown>, |
111 | | - TExtensions = ObjMap<unknown>, |
112 | | -> { |
113 | | - errors?: ReadonlyArray<GraphQLFormattedError>; |
114 | | - data: TData; |
115 | | - id: string; |
116 | | - subPath?: ReadonlyArray<string | number>; |
117 | | - extensions?: TExtensions; |
118 | | -} |
119 | | - |
120 | | -interface BareStreamItemsResult<TData = ReadonlyArray<unknown>> { |
121 | | - errors?: ReadonlyArray<GraphQLError>; |
122 | | - items: TData; |
123 | | -} |
124 | | - |
125 | | -export interface IncrementalStreamResult< |
126 | | - TData = ReadonlyArray<unknown>, |
127 | | - TExtensions = ObjMap<unknown>, |
128 | | -> extends BareStreamItemsResult<TData> { |
129 | | - id: string; |
130 | | - subPath?: ReadonlyArray<string | number>; |
131 | | - extensions?: TExtensions; |
132 | | -} |
133 | | - |
134 | | -export interface FormattedIncrementalStreamResult< |
135 | | - TData = Array<unknown>, |
136 | | - TExtensions = ObjMap<unknown>, |
137 | | -> { |
138 | | - errors?: ReadonlyArray<GraphQLFormattedError>; |
139 | | - items: TData; |
140 | | - id: string; |
141 | | - subPath?: ReadonlyArray<string | number>; |
142 | | - extensions?: TExtensions; |
143 | | -} |
144 | | - |
145 | | -export type IncrementalResult<TData = unknown, TExtensions = ObjMap<unknown>> = |
146 | | - | IncrementalDeferResult<TData, TExtensions> |
147 | | - | IncrementalStreamResult<TData, TExtensions>; |
148 | | - |
149 | | -export type FormattedIncrementalResult< |
150 | | - TData = unknown, |
151 | | - TExtensions = ObjMap<unknown>, |
152 | | -> = |
153 | | - | FormattedIncrementalDeferResult<TData, TExtensions> |
154 | | - | FormattedIncrementalStreamResult<TData, TExtensions>; |
155 | | - |
156 | | -export interface PendingResult { |
157 | | - id: string; |
158 | | - path: ReadonlyArray<string | number>; |
159 | | - label?: string; |
160 | | -} |
161 | | - |
162 | | -export interface CompletedResult { |
163 | | - id: string; |
164 | | - errors?: ReadonlyArray<GraphQLError>; |
165 | | -} |
166 | | - |
167 | | -export interface FormattedCompletedResult { |
168 | | - path: ReadonlyArray<string | number>; |
169 | | - label?: string; |
170 | | - errors?: ReadonlyArray<GraphQLError>; |
171 | | -} |
| 9 | +import type { |
| 10 | + CancellableStreamRecord, |
| 11 | + CompletedResult, |
| 12 | + DeferredFragmentRecord, |
| 13 | + DeferredGroupedFieldSetResult, |
| 14 | + ExperimentalIncrementalExecutionResults, |
| 15 | + IncrementalDataRecord, |
| 16 | + IncrementalDataRecordResult, |
| 17 | + IncrementalDeferResult, |
| 18 | + IncrementalResult, |
| 19 | + IncrementalStreamResult, |
| 20 | + InitialIncrementalExecutionResult, |
| 21 | + PendingResult, |
| 22 | + StreamItemsResult, |
| 23 | + SubsequentIncrementalExecutionResult, |
| 24 | + SubsequentResultRecord, |
| 25 | +} from './types.js'; |
| 26 | +import { |
| 27 | + isCancellableStreamRecord, |
| 28 | + isDeferredFragmentRecord, |
| 29 | + isDeferredGroupedFieldSetRecord, |
| 30 | + isDeferredGroupedFieldSetResult, |
| 31 | + isNonReconcilableDeferredGroupedFieldSetResult, |
| 32 | +} from './types.js'; |
172 | 33 |
|
173 | 34 | export function buildIncrementalResponse( |
174 | 35 | context: IncrementalPublisherContext, |
@@ -657,138 +518,3 @@ class IncrementalPublisher { |
657 | 518 | }; |
658 | 519 | } |
659 | 520 | } |
660 | | - |
661 | | -function isDeferredFragmentRecord( |
662 | | - subsequentResultRecord: SubsequentResultRecord, |
663 | | -): subsequentResultRecord is DeferredFragmentRecord { |
664 | | - return 'parent' in subsequentResultRecord; |
665 | | -} |
666 | | - |
667 | | -function isDeferredGroupedFieldSetRecord( |
668 | | - incrementalDataRecord: IncrementalDataRecord, |
669 | | -): incrementalDataRecord is DeferredGroupedFieldSetRecord { |
670 | | - return 'deferredFragmentRecords' in incrementalDataRecord; |
671 | | -} |
672 | | - |
673 | | -export type DeferredGroupedFieldSetResult = |
674 | | - | ReconcilableDeferredGroupedFieldSetResult |
675 | | - | NonReconcilableDeferredGroupedFieldSetResult; |
676 | | - |
677 | | -function isDeferredGroupedFieldSetResult( |
678 | | - subsequentResult: DeferredGroupedFieldSetResult | StreamItemsResult, |
679 | | -): subsequentResult is DeferredGroupedFieldSetResult { |
680 | | - return 'deferredFragmentRecords' in subsequentResult; |
681 | | -} |
682 | | - |
683 | | -interface ReconcilableDeferredGroupedFieldSetResult { |
684 | | - deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>; |
685 | | - path: Array<string | number>; |
686 | | - result: BareDeferredGroupedFieldSetResult; |
687 | | - incrementalDataRecords: ReadonlyArray<IncrementalDataRecord> | undefined; |
688 | | - sent?: true | undefined; |
689 | | - errors?: never; |
690 | | -} |
691 | | - |
692 | | -interface NonReconcilableDeferredGroupedFieldSetResult { |
693 | | - errors: ReadonlyArray<GraphQLError>; |
694 | | - deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>; |
695 | | - path: Array<string | number>; |
696 | | - result?: never; |
697 | | -} |
698 | | - |
699 | | -function isNonReconcilableDeferredGroupedFieldSetResult( |
700 | | - deferredGroupedFieldSetResult: DeferredGroupedFieldSetResult, |
701 | | -): deferredGroupedFieldSetResult is NonReconcilableDeferredGroupedFieldSetResult { |
702 | | - return deferredGroupedFieldSetResult.errors !== undefined; |
703 | | -} |
704 | | - |
705 | | -export interface DeferredGroupedFieldSetRecord { |
706 | | - deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>; |
707 | | - result: PromiseOrValue<DeferredGroupedFieldSetResult>; |
708 | | -} |
709 | | - |
710 | | -export interface SubsequentResultRecord { |
711 | | - path: Path | undefined; |
712 | | - label: string | undefined; |
713 | | - id?: string | undefined; |
714 | | -} |
715 | | - |
716 | | -/** @internal */ |
717 | | -export class DeferredFragmentRecord implements SubsequentResultRecord { |
718 | | - path: Path | undefined; |
719 | | - label: string | undefined; |
720 | | - id?: string | undefined; |
721 | | - parent: DeferredFragmentRecord | undefined; |
722 | | - expectedReconcilableResults: number; |
723 | | - results: Array<DeferredGroupedFieldSetResult>; |
724 | | - reconcilableResults: Array<ReconcilableDeferredGroupedFieldSetResult>; |
725 | | - children: Set<DeferredFragmentRecord>; |
726 | | - |
727 | | - constructor(opts: { |
728 | | - path: Path | undefined; |
729 | | - label: string | undefined; |
730 | | - parent: DeferredFragmentRecord | undefined; |
731 | | - }) { |
732 | | - this.path = opts.path; |
733 | | - this.label = opts.label; |
734 | | - this.parent = opts.parent; |
735 | | - this.expectedReconcilableResults = 0; |
736 | | - this.results = []; |
737 | | - this.reconcilableResults = []; |
738 | | - this.children = new Set(); |
739 | | - } |
740 | | -} |
741 | | - |
742 | | -export interface CancellableStreamRecord extends SubsequentResultRecord { |
743 | | - earlyReturn: () => Promise<unknown>; |
744 | | -} |
745 | | - |
746 | | -function isCancellableStreamRecord( |
747 | | - subsequentResultRecord: SubsequentResultRecord, |
748 | | -): subsequentResultRecord is CancellableStreamRecord { |
749 | | - return 'earlyReturn' in subsequentResultRecord; |
750 | | -} |
751 | | - |
752 | | -interface ReconcilableStreamItemsResult { |
753 | | - streamRecord: SubsequentResultRecord; |
754 | | - result: BareStreamItemsResult; |
755 | | - incrementalDataRecords: ReadonlyArray<IncrementalDataRecord> | undefined; |
756 | | - errors?: never; |
757 | | -} |
758 | | - |
759 | | -export function isReconcilableStreamItemsResult( |
760 | | - streamItemsResult: StreamItemsResult, |
761 | | -): streamItemsResult is ReconcilableStreamItemsResult { |
762 | | - return streamItemsResult.result !== undefined; |
763 | | -} |
764 | | - |
765 | | -interface TerminatingStreamItemsResult { |
766 | | - streamRecord: SubsequentResultRecord; |
767 | | - result?: never; |
768 | | - incrementalDataRecords?: never; |
769 | | - errors?: never; |
770 | | -} |
771 | | - |
772 | | -interface NonReconcilableStreamItemsResult { |
773 | | - streamRecord: SubsequentResultRecord; |
774 | | - errors: ReadonlyArray<GraphQLError>; |
775 | | - result?: never; |
776 | | -} |
777 | | - |
778 | | -export type StreamItemsResult = |
779 | | - | ReconcilableStreamItemsResult |
780 | | - | TerminatingStreamItemsResult |
781 | | - | NonReconcilableStreamItemsResult; |
782 | | - |
783 | | -export interface StreamItemsRecord { |
784 | | - streamRecord: SubsequentResultRecord; |
785 | | - result: PromiseOrValue<StreamItemsResult>; |
786 | | -} |
787 | | - |
788 | | -export type IncrementalDataRecord = |
789 | | - | DeferredGroupedFieldSetRecord |
790 | | - | StreamItemsRecord; |
791 | | - |
792 | | -export type IncrementalDataRecordResult = |
793 | | - | DeferredGroupedFieldSetResult |
794 | | - | StreamItemsResult; |
0 commit comments