Skip to content

Commit 4387ccd

Browse files
committed
add ExecutionPartialResultNP
1 parent 3f2608d commit 4387ccd

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
@@ -109,15 +109,21 @@ export type ExecutionResult = {
109109
};
110110

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

122128
export type ExecutionArgs = {|
123129
schema: GraphQLSchema,
@@ -240,18 +246,8 @@ function executeImpl(
240246
*/
241247
function mergeErrors(
242248
context: ExecutionContext,
243-
result: ExecutionPartialResult<mixed>,
244-
): ExecutionPartialResult<mixed> {
245-
invariant(!getPromise(result), 'Cannot call on a Promise.');
246-
// this contortion is to persuade Flow that it really really is a Promise
247-
if (
248-
result instanceof Promise ||
249-
(typeof result === 'object' &&
250-
result !== null &&
251-
typeof result.then === 'function')
252-
) {
253-
throw new Error('Cannot call on a Promise.');
254-
}
249+
result: ExecutionPartialResultNP<mixed>,
250+
): ExecutionPartialResultNP<mixed> {
255251
const errors = context.errors;
256252
context.errors = [];
257253
return { data: result.data, errors: [...errors, ...(result.errors || [])] };
@@ -598,8 +594,8 @@ function executeFields(
598594
}
599595

600596
function mergeEPRs(
601-
eprMap: ObjMap<ExecutionPartialResult<mixed>>,
602-
): ExecutionPartialResult<mixed> {
597+
eprMap: ObjMap<ExecutionPartialResultNP<mixed>>,
598+
): ExecutionPartialResultNP<mixed> {
603599
const keys = Object.keys(eprMap);
604600
const values = keys.map(name => eprMap[name]);
605601
return values.reduce(
@@ -743,9 +739,9 @@ function doesFragmentConditionMatch(
743739
* This is akin to bluebird's `Promise.props`, but implemented only using
744740
* `Promise.all` so it will work with any implementation of ES6 promises.
745741
*/
746-
function promiseForObject<T>(
747-
object: ObjMap<ExecutionPartialResult<T>>,
748-
): ExecutionPartialResult<T> {
742+
function promiseForObject(
743+
object: ObjMap<Promise<ExecutionPartialResultNP<mixed>>>,
744+
): Promise<ExecutionPartialResultNP<mixed>> {
749745
const keys = Object.keys(object);
750746
const valuesAndPromises = keys.map(name => object[name]);
751747
return Promise.all(valuesAndPromises)
@@ -1142,8 +1138,8 @@ function completeListValue(
11421138
}
11431139

11441140
function flattenEPRs(
1145-
eprs: $ReadOnlyArray<ExecutionPartialResult<mixed>>,
1146-
): ExecutionPartialResult<mixed> {
1141+
eprs: $ReadOnlyArray<ExecutionPartialResultNP<mixed>>,
1142+
): ExecutionPartialResultNP<mixed> {
11471143
const errors = [];
11481144
const data = [];
11491145
forEach((eprs: any), item => {

0 commit comments

Comments
 (0)