Skip to content

Commit 5ea3fde

Browse files
committed
add ExecutionPartialResultNP
1 parent 24a2c4a commit 5ea3fde

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/execution/execute.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,21 @@ export type ExecutionResult = {
110110
};
111111

112112
/**
113-
* A partial result of GraphQL execution.
114-
*
115-
* - `errors` is included when any errors occurred as a non-empty array.
116-
* - `data` is the result of a successful execution of the query.
113+
* A partial result of GraphQL execution. `data` and `errors` as above.
114+
* "NP" = "Not Promise".
117115
*/
118-
export type ExecutionPartialResult<T> = MaybePromise<{
116+
export type ExecutionPartialResultNP<T> = {
119117
errors?: $ReadOnlyArray<GraphQLError>,
120118
data?: T | null,
121-
}>;
119+
};
120+
121+
/**
122+
* A partial result of GraphQL execution. `data` and `errors` as above.
123+
* This version might be a promise.
124+
*/
125+
export type ExecutionPartialResult<T> = MaybePromise<
126+
ExecutionPartialResultNP<T>,
127+
>;
122128

123129
export type ExecutionArgs = {|
124130
schema: GraphQLSchema,
@@ -235,18 +241,8 @@ function executeImpl(
235241
*/
236242
function mergeErrors(
237243
context: ExecutionContext,
238-
result: ExecutionPartialResult<mixed>,
239-
): ExecutionPartialResult<mixed> {
240-
invariant(!getPromise(result), 'Cannot call on a Promise.');
241-
// this contortion is to persuade Flow that it really really is a Promise
242-
if (
243-
result instanceof Promise ||
244-
(typeof result === 'object' &&
245-
result !== null &&
246-
typeof result.then === 'function')
247-
) {
248-
throw new Error('Cannot call on a Promise.');
249-
}
244+
result: ExecutionPartialResultNP<mixed>,
245+
): ExecutionPartialResultNP<mixed> {
250246
const errors = context.errors;
251247
context.errors = [];
252248
return { data: result.data, errors: [...errors, ...(result.errors || [])] };
@@ -593,8 +589,8 @@ function executeFields(
593589
}
594590

595591
function mergeEPRs(
596-
eprMap: ObjMap<ExecutionPartialResult<mixed>>,
597-
): ExecutionPartialResult<mixed> {
592+
eprMap: ObjMap<ExecutionPartialResultNP<mixed>>,
593+
): ExecutionPartialResultNP<mixed> {
598594
const keys = Object.keys(eprMap);
599595
const values = keys.map(name => eprMap[name]);
600596
return values.reduce(
@@ -738,9 +734,9 @@ function doesFragmentConditionMatch(
738734
* This is akin to bluebird's `Promise.props`, but implemented only using
739735
* `Promise.all` so it will work with any implementation of ES6 promises.
740736
*/
741-
function promiseForObject<T>(
742-
object: ObjMap<ExecutionPartialResult<T>>,
743-
): ExecutionPartialResult<T> {
737+
function promiseForObject(
738+
object: ObjMap<Promise<ExecutionPartialResultNP<mixed>>>,
739+
): Promise<ExecutionPartialResultNP<mixed>> {
744740
const keys = Object.keys(object);
745741
const valuesAndPromises = keys.map(name => object[name]);
746742
return Promise.all(valuesAndPromises)
@@ -1137,8 +1133,8 @@ function completeListValue(
11371133
}
11381134

11391135
function flattenEPRs(
1140-
eprs: $ReadOnlyArray<ExecutionPartialResult<mixed>>,
1141-
): ExecutionPartialResult<mixed> {
1136+
eprs: $ReadOnlyArray<ExecutionPartialResultNP<mixed>>,
1137+
): ExecutionPartialResultNP<mixed> {
11421138
const errors = [];
11431139
const data = [];
11441140
forEach((eprs: any), item => {

0 commit comments

Comments
 (0)