From fd849cb0c6de7e4dece7efe9007ec285c1b597c8 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Thu, 22 Aug 2019 14:01:45 -0700 Subject: [PATCH] Sync execution TS definitions with Flow. Specifically: Create jsutils/Path.d.ts and pull some utility funcs out to there Add typeResolver options --- tstypes/execution/execute.d.ts | 59 +++++++++++++++++----------------- tstypes/execution/index.d.ts | 4 ++- tstypes/execution/values.d.ts | 23 ++++++------- tstypes/jsutils/Path.d.ts | 14 ++++++++ 4 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 tstypes/jsutils/Path.d.ts diff --git a/tstypes/execution/execute.d.ts b/tstypes/execution/execute.d.ts index e6e4632911..ad8dd34e4f 100644 --- a/tstypes/execution/execute.d.ts +++ b/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, @@ -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. @@ -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 { errors?: ReadonlyArray; - data?: TData; + data?: TData | null; } export type ExecutionArgs = { @@ -59,6 +62,7 @@ export type ExecutionArgs = { variableValues?: Maybe<{ [key: string]: any }>; operationName?: Maybe; fieldResolver?: Maybe>; + typeResolver?: Maybe>; }; /** @@ -84,26 +88,9 @@ export function execute( variableValues?: Maybe<{ [key: string]: any }>, operationName?: Maybe, fieldResolver?: Maybe>, + typeResolver?: Maybe>, ): PromiseOrValue>; -/** - * 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; - -/** - * 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. @@ -128,6 +115,7 @@ export function buildExecutionContext( rawVariableValues: Maybe<{ [key: string]: any }>, operationName: Maybe, fieldResolver: Maybe>, + typeResolver?: Maybe>, ): ReadonlyArray | ExecutionContext; /** @@ -151,11 +139,12 @@ export function buildResolveInfo( fieldDef: GraphQLField, fieldNodes: ReadonlyArray, 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( exeContext: ExecutionContext, fieldDef: GraphQLField, @@ -165,6 +154,18 @@ export function resolveFieldValueOrError( 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; + /** * 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 diff --git a/tstypes/execution/index.d.ts b/tstypes/execution/index.d.ts index a5c4980c0a..ed0f8f1808 100644 --- a/tstypes/execution/index.d.ts +++ b/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'; diff --git a/tstypes/execution/values.d.ts b/tstypes/execution/values.d.ts index 3a7398f76f..5af3e040db 100644 --- a/tstypes/execution/values.d.ts +++ b/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 | 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; coerced?: never } + | { errors?: never; coerced: { [key: string]: any } }; /** * Prepares an object map of variableValues of the correct type based on the @@ -31,6 +31,7 @@ export function getVariableValues( schema: GraphQLSchema, varDefNodes: VariableDefinitionNode[], inputs: { [key: string]: any }, + options: { maxErrors?: number }, ): CoercedVariableValues; /** diff --git a/tstypes/jsutils/Path.d.ts b/tstypes/jsutils/Path.d.ts new file mode 100644 index 0000000000..c53422820d --- /dev/null +++ b/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;