Skip to content

Commit 329f357

Browse files
Rename 'MaybePromise' to 'PromiseOrValue' (#1798)
Suggested by @martijnwalraven `MaybePromise` name confuses users into thinking: `execute`/`graphql` returns either Promise or null/undefined. Also in our codebase we use `[Mm]aybe*` to represent optional things, e.g.: https://github.com/graphql/graphql-js/blob/8c96dc8276f2de27b8af9ffbd71a4597d483523f/src/language/visitor.js#L353 https://github.com/graphql/graphql-js/blob/8c96dc8276f2de27b8af9ffbd71a4597d483523f/src/language/printer.js#L244
1 parent 84ea1c9 commit 329f357

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

src/execution/execute.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import memoize3 from '../jsutils/memoize3';
1919
import promiseForObject from '../jsutils/promiseForObject';
2020
import promiseReduce from '../jsutils/promiseReduce';
2121
import type { ObjMap } from '../jsutils/ObjMap';
22-
import type { MaybePromise } from '../jsutils/MaybePromise';
22+
import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
2323

2424
import { getOperationRootType } from '../utilities/getOperationRootType';
2525
import { typeFromAST } from '../utilities/typeFromAST';
@@ -144,7 +144,7 @@ export type ExecutionArgs = {|
144144
declare function execute(
145145
ExecutionArgs,
146146
..._: []
147-
): MaybePromise<ExecutionResult>;
147+
): PromiseOrValue<ExecutionResult>;
148148
/* eslint-disable no-redeclare */
149149
declare function execute(
150150
schema: GraphQLSchema,
@@ -155,7 +155,7 @@ declare function execute(
155155
operationName?: ?string,
156156
fieldResolver?: ?GraphQLFieldResolver<any, any>,
157157
typeResolver?: ?GraphQLTypeResolver<any, any>,
158-
): MaybePromise<ExecutionResult>;
158+
): PromiseOrValue<ExecutionResult>;
159159
export function execute(
160160
argsOrSchema,
161161
document,
@@ -239,7 +239,7 @@ function executeImpl(
239239
*/
240240
function buildResponse(
241241
exeContext: ExecutionContext,
242-
data: MaybePromise<ObjMap<mixed> | null>,
242+
data: PromiseOrValue<ObjMap<mixed> | null>,
243243
) {
244244
if (isPromise(data)) {
245245
return data.then(resolved => buildResponse(exeContext, resolved));
@@ -393,7 +393,7 @@ function executeOperation(
393393
exeContext: ExecutionContext,
394394
operation: OperationDefinitionNode,
395395
rootValue: mixed,
396-
): MaybePromise<ObjMap<mixed> | null> {
396+
): PromiseOrValue<ObjMap<mixed> | null> {
397397
const type = getOperationRootType(exeContext.schema, operation);
398398
const fields = collectFields(
399399
exeContext,
@@ -438,7 +438,7 @@ function executeFieldsSerially(
438438
sourceValue: mixed,
439439
path: ResponsePath | void,
440440
fields: ObjMap<Array<FieldNode>>,
441-
): MaybePromise<ObjMap<mixed>> {
441+
): PromiseOrValue<ObjMap<mixed>> {
442442
return promiseReduce(
443443
Object.keys(fields),
444444
(results, responseName) => {
@@ -477,7 +477,7 @@ function executeFields(
477477
sourceValue: mixed,
478478
path: ResponsePath | void,
479479
fields: ObjMap<Array<FieldNode>>,
480-
): MaybePromise<ObjMap<mixed>> {
480+
): PromiseOrValue<ObjMap<mixed>> {
481481
const results = Object.create(null);
482482
let containsPromise = false;
483483

@@ -653,7 +653,7 @@ function resolveField(
653653
source: mixed,
654654
fieldNodes: $ReadOnlyArray<FieldNode>,
655655
path: ResponsePath,
656-
): MaybePromise<mixed> {
656+
): PromiseOrValue<mixed> {
657657
const fieldNode = fieldNodes[0];
658658
const fieldName = fieldNode.name.value;
659659

@@ -766,7 +766,7 @@ function completeValueCatchingError(
766766
info: GraphQLResolveInfo,
767767
path: ResponsePath,
768768
result: mixed,
769-
): MaybePromise<mixed> {
769+
): PromiseOrValue<mixed> {
770770
try {
771771
let completed;
772772
if (isPromise(result)) {
@@ -844,7 +844,7 @@ function completeValue(
844844
info: GraphQLResolveInfo,
845845
path: ResponsePath,
846846
result: mixed,
847-
): MaybePromise<mixed> {
847+
): PromiseOrValue<mixed> {
848848
// If result is an Error, throw a located error.
849849
if (result instanceof Error) {
850850
throw result;
@@ -939,7 +939,7 @@ function completeListValue(
939939
info: GraphQLResolveInfo,
940940
path: ResponsePath,
941941
result: mixed,
942-
): MaybePromise<$ReadOnlyArray<mixed>> {
942+
): PromiseOrValue<$ReadOnlyArray<mixed>> {
943943
invariant(
944944
isCollection(result),
945945
`Expected Iterable, but did not find one for field ${
@@ -1001,7 +1001,7 @@ function completeAbstractValue(
10011001
info: GraphQLResolveInfo,
10021002
path: ResponsePath,
10031003
result: mixed,
1004-
): MaybePromise<ObjMap<mixed>> {
1004+
): PromiseOrValue<ObjMap<mixed>> {
10051005
const resolveTypeFn = returnType.resolveType || exeContext.typeResolver;
10061006
const contextValue = exeContext.contextValue;
10071007
const runtimeType = resolveTypeFn(result, contextValue, info, returnType);
@@ -1088,7 +1088,7 @@ function completeObjectValue(
10881088
info: GraphQLResolveInfo,
10891089
path: ResponsePath,
10901090
result: mixed,
1091-
): MaybePromise<ObjMap<mixed>> {
1091+
): PromiseOrValue<ObjMap<mixed>> {
10921092
// If there is an isTypeOf predicate function, call it with the
10931093
// current result. If isTypeOf returns false, then raise an error rather
10941094
// than continuing execution.
@@ -1141,7 +1141,7 @@ function collectAndExecuteSubfields(
11411141
fieldNodes: $ReadOnlyArray<FieldNode>,
11421142
path: ResponsePath,
11431143
result: mixed,
1144-
): MaybePromise<ObjMap<mixed>> {
1144+
): PromiseOrValue<ObjMap<mixed>> {
11451145
// Collect sub-fields to execute to complete this value.
11461146
const subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes);
11471147
return executeFields(exeContext, returnType, result, path, subFieldNodes);

src/graphql.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
} from './type/definition';
2020
import type { GraphQLSchema } from './type/schema';
2121
import type { ExecutionResult } from './execution/execute';
22-
import type { MaybePromise } from './jsutils/MaybePromise';
22+
import type { PromiseOrValue } from './jsutils/PromiseOrValue';
2323

2424
/**
2525
* This is the primary entry point function for fulfilling GraphQL operations
@@ -188,7 +188,7 @@ function graphqlImpl(
188188
operationName,
189189
fieldResolver,
190190
typeResolver,
191-
): MaybePromise<ExecutionResult> {
191+
): PromiseOrValue<ExecutionResult> {
192192
// Validate Schema
193193
const schemaValidationErrors = validateSchema(schema);
194194
if (schemaValidationErrors.length > 0) {

src/jsutils/MaybePromise.js renamed to src/jsutils/PromiseOrValue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
* @flow strict
88
*/
99

10-
export type MaybePromise<+T> = Promise<T> | T;
10+
export type PromiseOrValue<+T> = Promise<T> | T;

src/jsutils/promiseReduce.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import isPromise from './isPromise';
11-
import type { MaybePromise } from './MaybePromise';
11+
import type { PromiseOrValue } from './PromiseOrValue';
1212

1313
/**
1414
* Similar to Array.prototype.reduce(), however the reducing callback may return
@@ -19,9 +19,9 @@ import type { MaybePromise } from './MaybePromise';
1919
*/
2020
export default function promiseReduce<T, U>(
2121
values: $ReadOnlyArray<T>,
22-
callback: (U, T) => MaybePromise<U>,
23-
initialValue: MaybePromise<U>,
24-
): MaybePromise<U> {
22+
callback: (U, T) => PromiseOrValue<U>,
23+
initialValue: PromiseOrValue<U>,
24+
): PromiseOrValue<U> {
2525
return values.reduce(
2626
(previous, value) =>
2727
isPromise(previous)

src/subscription/mapAsyncIterator.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
*/
99

1010
import { $$asyncIterator, getAsyncIterator } from 'iterall';
11-
import type { MaybePromise } from '../jsutils/MaybePromise';
11+
import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
1212

1313
/**
1414
* Given an AsyncIterable and a callback function, return an AsyncIterator
1515
* which produces values mapped via calling the callback function.
1616
*/
1717
export default function mapAsyncIterator<T, U>(
1818
iterable: AsyncIterable<T>,
19-
callback: T => MaybePromise<U>,
20-
rejectCallback?: any => MaybePromise<U>,
19+
callback: T => PromiseOrValue<U>,
20+
rejectCallback?: any => PromiseOrValue<U>,
2121
): AsyncGenerator<U, void, void> {
2222
const iterator = getAsyncIterator(iterable);
2323
let $return;
@@ -71,7 +71,7 @@ export default function mapAsyncIterator<T, U>(
7171

7272
function asyncMapValue<T, U>(
7373
value: T,
74-
callback: T => MaybePromise<U>,
74+
callback: T => PromiseOrValue<U>,
7575
): Promise<U> {
7676
return new Promise(resolve => resolve(callback(value)));
7777
}

src/type/definition.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import type {
4141
ValueNode,
4242
} from '../language/ast';
4343
import type { GraphQLSchema } from './schema';
44-
import type { MaybePromise } from '../jsutils/MaybePromise';
44+
import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
4545

4646
// Predicates & Assertions
4747

@@ -839,13 +839,13 @@ export type GraphQLTypeResolver<TSource, TContext> = (
839839
context: TContext,
840840
info: GraphQLResolveInfo,
841841
abstractType: GraphQLAbstractType,
842-
) => MaybePromise<?GraphQLObjectType | string>;
842+
) => PromiseOrValue<?GraphQLObjectType | string>;
843843

844844
export type GraphQLIsTypeOfFn<TSource, TContext> = (
845845
source: TSource,
846846
context: TContext,
847847
info: GraphQLResolveInfo,
848-
) => MaybePromise<boolean>;
848+
) => PromiseOrValue<boolean>;
849849

850850
export type GraphQLFieldResolver<
851851
TSource,

0 commit comments

Comments
 (0)