Skip to content

RFC: More flow-friendly thenable checking. #699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,9 @@ function executeFieldsSerially(
if (result === undefined) {
return results;
}
if (isThenable(result)) {
return ((result: any): Promise<*>).then(resolvedResult => {
const promise = getPromise(result);
if (promise) {
return promise.then(resolvedResult => {
results[responseName] = resolvedResult;
return results;
});
Expand Down Expand Up @@ -382,7 +383,7 @@ function executeFields(
return results;
}
results[responseName] = result;
if (isThenable(result)) {
if (getPromise(result)) {
containsPromise = true;
}
return results;
Expand Down Expand Up @@ -688,12 +689,13 @@ function completeValueCatchingError(
path,
result
);
if (isThenable(completed)) {
const promise = getPromise(completed);
if (promise) {
// If `completeValueWithLocatedError` returned a rejected promise, log
// the rejection error and resolve to null.
// Note: we don't rely on a `catch` method, but we do expect "thenable"
// to take a second callback for the error case.
return ((completed: any): Promise<*>).then(undefined, error => {
return promise.then(undefined, error => {
exeContext.errors.push(error);
return Promise.resolve(null);
});
Expand Down Expand Up @@ -726,8 +728,9 @@ function completeValueWithLocatedError(
path,
result
);
if (isThenable(completed)) {
return ((completed: any): Promise<*>).then(
const promise = getPromise(completed);
if (promise) {
return promise.then(
undefined,
error => Promise.reject(
locatedError(error, fieldNodes, responsePathAsArray(path))
Expand Down Expand Up @@ -770,8 +773,9 @@ function completeValue(
result: mixed
): mixed {
// If result is a Promise, apply-lift over completeValue.
if (isThenable(result)) {
return ((result: any): Promise<*>).then(
const promise = getPromise(result);
if (promise) {
return promise.then(
resolved => completeValue(
exeContext,
returnType,
Expand Down Expand Up @@ -900,7 +904,7 @@ function completeListValue(
item
);

if (!containsPromise && isThenable(completedItem)) {
if (!containsPromise && getPromise(completedItem)) {
containsPromise = true;
}
completedResults.push(completedItem);
Expand Down Expand Up @@ -944,11 +948,9 @@ function completeAbstractValue(
returnType.resolveType(result, exeContext.contextValue, info) :
defaultResolveTypeFn(result, exeContext.contextValue, info, returnType);

if (isThenable(runtimeType)) {
// Cast to Promise
const runtimeTypePromise: Promise<?GraphQLObjectType | string> =
(runtimeType: any);
return runtimeTypePromise.then(resolvedRuntimeType =>
const promise = getPromise(runtimeType);
if (promise) {
return promise.then(resolvedRuntimeType =>
completeObjectValue(
exeContext,
ensureValidRuntimeType(
Expand Down Expand Up @@ -1033,8 +1035,9 @@ function completeObjectValue(
if (returnType.isTypeOf) {
const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info);

if (isThenable(isTypeOf)) {
return ((isTypeOf: any): Promise<boolean>).then(isTypeOfResult => {
const promise = getPromise(isTypeOf);
if (promise) {
return promise.then(isTypeOfResult => {
if (!isTypeOfResult) {
throw invalidReturnTypeError(returnType, result, fieldNodes);
}
Expand Down Expand Up @@ -1122,8 +1125,9 @@ function defaultResolveTypeFn(
if (type.isTypeOf) {
const isTypeOfResult = type.isTypeOf(value, context, info);

if (isThenable(isTypeOfResult)) {
promisedIsTypeOfResults[i] = isTypeOfResult;
const promise = getPromise(isTypeOfResult);
if (promise) {
promisedIsTypeOfResults[i] = promise;
} else if (isTypeOfResult) {
return type;
}
Expand Down Expand Up @@ -1160,13 +1164,15 @@ function (source, args, context, info) {
};

/**
* Checks to see if this object acts like a Promise, i.e. has a "then"
* function.
* Only returns the value if it acts like a Promise, i.e. has a "then" function,
* otherwise returns void.
*/
function isThenable(value: mixed): boolean {
return typeof value === 'object' &&
value !== null &&
typeof value.then === 'function';
function getPromise<T>(value: Promise<T> | mixed): Promise<T> | void {
if (typeof value === 'object' &&
value !== null &&
typeof value.then === 'function') {
return (value: any);
}
}

/**
Expand Down