Skip to content

Commit

Permalink
Sync execution TS definitions with Flow.
Browse files Browse the repository at this point in the history
Specifically:
Create jsutils/Path.d.ts and pull some utility funcs out to there
Add typeResolver options
  • Loading branch information
Jackson Kearl authored and IvanGoncharov committed Aug 23, 2019
1 parent 37161b2 commit 7d9ad14
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
59 changes: 30 additions & 29 deletions tstypes/execution/execute.d.ts
@@ -1,13 +1,8 @@
import Maybe from '../tsutils/Maybe';
import { PromiseOrValue } from '../jsutils/PromiseOrValue';
import { Path, addPath, pathToArray } from '../jsutils/Path';

import { GraphQLError, locatedError } from '../error';
import { GraphQLSchema } from '../type/schema';
import {
GraphQLField,
GraphQLFieldResolver,
ResponsePath,
GraphQLObjectType,
GraphQLResolveInfo,
} from '../type/definition';
import {
DirectiveNode,
DocumentNode,
Expand All @@ -17,7 +12,14 @@ import {
InlineFragmentNode,
FragmentDefinitionNode,
} from '../language/ast';
import { PromiseOrValue } from '../jsutils/PromiseOrValue';
import { GraphQLSchema } from '../type/schema';
import {
GraphQLField,
GraphQLFieldResolver,
GraphQLResolveInfo,
GraphQLTypeResolver,
GraphQLObjectType,
} from '../type/definition';

/**
* Data that must be available at all points during query execution.
Expand Down Expand Up @@ -46,9 +48,10 @@ export interface ExecutionResultDataDefault {
* - `errors` is included when any errors occurred as a non-empty array.
* - `data` is the result of a successful execution of the query.
*/
// TS_SPECIFIC: TData and ExecutionResultDataDefault
export interface ExecutionResult<TData = ExecutionResultDataDefault> {
errors?: ReadonlyArray<GraphQLError>;
data?: TData;
data?: TData | null;
}

export type ExecutionArgs = {
Expand All @@ -59,6 +62,7 @@ export type ExecutionArgs = {
variableValues?: Maybe<{ [key: string]: any }>;
operationName?: Maybe<string>;
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
};

/**
Expand All @@ -84,26 +88,9 @@ export function execute<TData = ExecutionResultDataDefault>(
variableValues?: Maybe<{ [key: string]: any }>,
operationName?: Maybe<string>,
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
): PromiseOrValue<ExecutionResult<TData>>;

/**
* Given a ResponsePath (found in the `path` entry in the information provided
* as the last argument to a field resolver), return an Array of the path keys.
*/
export function responsePathAsArray(
path: ResponsePath,
): ReadonlyArray<string | number>;

/**
* Given a ResponsePath and a key, return a new ResponsePath containing the
* new key.
*/
export function addPath(
prev: ResponsePath | undefined,
key: string | number,
): { prev: ResponsePath | undefined; key: string | number };

/**
* Essential assertions before executing to provide developer feedback for
* improper use of the GraphQL library.
Expand All @@ -128,6 +115,7 @@ export function buildExecutionContext(
rawVariableValues: Maybe<{ [key: string]: any }>,
operationName: Maybe<string>,
fieldResolver: Maybe<GraphQLFieldResolver<any, any>>,
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
): ReadonlyArray<GraphQLError> | ExecutionContext;

/**
Expand All @@ -151,11 +139,12 @@ export function buildResolveInfo(
fieldDef: GraphQLField<any, any>,
fieldNodes: ReadonlyArray<FieldNode>,
parentType: GraphQLObjectType,
path: ResponsePath,
path: Path,
): GraphQLResolveInfo;

// Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField`
// function. Returns the result of resolveFn or the abrupt-return Error object.
// TS_SPECIFIC: TSource
export function resolveFieldValueOrError<TSource>(
exeContext: ExecutionContext,
fieldDef: GraphQLField<TSource, any>,
Expand All @@ -165,6 +154,18 @@ export function resolveFieldValueOrError<TSource>(
info: GraphQLResolveInfo,
): Error | any;

/**
* If a resolveType function is not given, then a default resolve behavior is
* used which attempts two strategies:
*
* First, See if the provided value has a `__typename` field defined, if so, use
* that value as name of the resolved type.
*
* Otherwise, test each possible type for the abstract type by calling
* isTypeOf for the object being coerced, returning the first type that matches.
*/
export const defaultTypeResolver: GraphQLTypeResolver<any, any>;

/**
* If a resolve function is not given, then a default resolve behavior is used
* which takes the property of the source object of the same name as the field
Expand Down
4 changes: 3 additions & 1 deletion tstypes/execution/index.d.ts
@@ -1,7 +1,9 @@
export { pathToArray as responsePathAsArray } from '../jsutils/Path';

export {
execute,
defaultFieldResolver,
responsePathAsArray,
defaultTypeResolver,
ExecutionArgs,
ExecutionResult,
} from './execute';
Expand Down
23 changes: 12 additions & 11 deletions tstypes/execution/values.d.ts
@@ -1,22 +1,22 @@
import Maybe from '../tsutils/Maybe';
import { GraphQLError } from '../error/GraphQLError';
import {
GraphQLInputType,
GraphQLField,
GraphQLArgument,
} from '../type/definition';
import { GraphQLDirective } from '../type/directives';
import { GraphQLSchema } from '../type/schema';
import {
FieldNode,
DirectiveNode,
VariableDefinitionNode,
} from '../language/ast';

interface CoercedVariableValues {
errors: ReadonlyArray<GraphQLError> | undefined;
coerced: { [key: string]: any } | undefined;
}
import { GraphQLDirective } from '../type/directives';
import { GraphQLSchema } from '../type/schema';
import {
GraphQLInputType,
GraphQLField,
GraphQLArgument,
} from '../type/definition';

type CoercedVariableValues =
| { errors: ReadonlyArray<GraphQLError>; coerced?: never }
| { errors?: never; coerced: { [key: string]: any } };

/**
* Prepares an object map of variableValues of the correct type based on the
Expand All @@ -31,6 +31,7 @@ export function getVariableValues(
schema: GraphQLSchema,
varDefNodes: VariableDefinitionNode[],
inputs: { [key: string]: any },
options: { maxErrors?: number },
): CoercedVariableValues;

/**
Expand Down
14 changes: 14 additions & 0 deletions tstypes/jsutils/Path.d.ts
@@ -0,0 +1,14 @@
export type Path = {
prev: Path | void;
key: string | number;
};

/**
* Given a Path and a key, return a new Path containing the new key.
*/
export function addPath(prev: Path | undefined, key: string | number): Path;

/**
* Given a Path, return an Array of the path keys.
*/
export function pathToArray(path: Path): Array<string | number>;

0 comments on commit 7d9ad14

Please sign in to comment.