Skip to content

DRY with MaybePromise #1164

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
Dec 19, 2017
Merged
Show file tree
Hide file tree
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
11 changes: 6 additions & 5 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import invariant from '../jsutils/invariant';
import isInvalid from '../jsutils/isInvalid';
import isNullish from '../jsutils/isNullish';
import type { ObjMap } from '../jsutils/ObjMap';
import type { MaybePromise } from '../jsutils/MaybePromise';

import { typeFromAST } from '../utilities/typeFromAST';
import * as Kind from '../language/kinds';
Expand Down Expand Up @@ -133,7 +134,7 @@ export type ExecutionArgs = {|
declare function execute(
ExecutionArgs,
..._: []
): Promise<ExecutionResult> | ExecutionResult;
): MaybePromise<ExecutionResult>;
/* eslint-disable no-redeclare */
declare function execute(
schema: GraphQLSchema,
Expand All @@ -143,7 +144,7 @@ declare function execute(
variableValues?: ?{ [variable: string]: mixed },
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>,
): Promise<ExecutionResult> | ExecutionResult;
): MaybePromise<ExecutionResult>;
export function execute(
argsOrSchema,
document,
Expand Down Expand Up @@ -222,7 +223,7 @@ function executeImpl(
*/
function buildResponse(
context: ExecutionContext,
data: Promise<ObjMap<mixed> | null> | ObjMap<mixed> | null,
data: MaybePromise<ObjMap<mixed> | null>,
) {
const promise = getPromise(data);
if (promise) {
Expand Down Expand Up @@ -376,7 +377,7 @@ function executeOperation(
exeContext: ExecutionContext,
operation: OperationDefinitionNode,
rootValue: mixed,
): Promise<ObjMap<mixed> | null> | ObjMap<mixed> | null {
): MaybePromise<ObjMap<mixed> | null> {
const type = getOperationRootType(exeContext.schema, operation);
const fields = collectFields(
exeContext,
Expand Down Expand Up @@ -503,7 +504,7 @@ function executeFields(
sourceValue: mixed,
path: ResponsePath | void,
fields: ObjMap<Array<FieldNode>>,
): Promise<ObjMap<mixed>> | ObjMap<mixed> {
): MaybePromise<ObjMap<mixed>> {
let containsPromise = false;

const finalResults = Object.keys(fields).reduce((results, responseName) => {
Expand Down
3 changes: 2 additions & 1 deletion src/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { Source } from './language/source';
import type { GraphQLFieldResolver } from './type/definition';
import type { GraphQLSchema } from './type/schema';
import type { ExecutionResult } from './execution/execute';
import type { MaybePromise } from './jsutils/MaybePromise';

/**
* This is the primary entry point function for fulfilling GraphQL operations
Expand Down Expand Up @@ -168,7 +169,7 @@ function graphqlImpl(
variableValues,
operationName,
fieldResolver,
): Promise<ExecutionResult> | ExecutionResult {
): MaybePromise<ExecutionResult> {
// Validate Schema
const schemaValidationErrors = validateSchema(schema);
if (schemaValidationErrors.length > 0) {
Expand Down
10 changes: 10 additions & 0 deletions src/jsutils/MaybePromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export type MaybePromise<T> = Promise<T> | T;
7 changes: 4 additions & 3 deletions src/subscription/mapAsyncIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
*/

import { $$asyncIterator, getAsyncIterator } from 'iterall';
import type { MaybePromise } from '../jsutils/MaybePromise';

/**
* Given an AsyncIterable and a callback function, return an AsyncIterator
* which produces values mapped via calling the callback function.
*/
export default function mapAsyncIterator<T, U>(
iterable: AsyncIterable<T>,
callback: T => Promise<U> | U,
rejectCallback?: any => Promise<U> | U,
callback: T => MaybePromise<U>,
rejectCallback?: any => MaybePromise<U>,
): AsyncGenerator<U, void, void> {
const iterator = getAsyncIterator(iterable);
let $return;
Expand Down Expand Up @@ -68,7 +69,7 @@ export default function mapAsyncIterator<T, U>(

function asyncMapValue<T, U>(
value: T,
callback: T => Promise<U> | U,
callback: T => MaybePromise<U>,
): Promise<U> {
return new Promise(resolve => resolve(callback(value)));
}
Expand Down
5 changes: 3 additions & 2 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
ValueNode,
} from '../language/ast';
import type { GraphQLSchema } from './schema';
import type { MaybePromise } from '../jsutils/MaybePromise';
import { GraphQLList, GraphQLNonNull } from './wrappers';

// Predicates & Assertions
Expand Down Expand Up @@ -705,13 +706,13 @@ export type GraphQLTypeResolver<TSource, TContext> = (
value: TSource,
context: TContext,
info: GraphQLResolveInfo,
) => ?GraphQLObjectType | string | Promise<?GraphQLObjectType | string>;
) => MaybePromise<?GraphQLObjectType | string>;

export type GraphQLIsTypeOfFn<TSource, TContext> = (
source: TSource,
context: TContext,
info: GraphQLResolveInfo,
) => boolean | Promise<boolean>;
) => MaybePromise<boolean>;

export type GraphQLFieldResolver<TSource, TContext> = (
source: TSource,
Expand Down