Skip to content

Commit 7d9ad14

Browse files
Jackson KearlIvanGoncharov
Jackson Kearl
authored andcommitted
Sync execution TS definitions with Flow.
Specifically: Create jsutils/Path.d.ts and pull some utility funcs out to there Add typeResolver options
1 parent 37161b2 commit 7d9ad14

File tree

4 files changed

+59
-41
lines changed

4 files changed

+59
-41
lines changed

tstypes/execution/execute.d.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import Maybe from '../tsutils/Maybe';
2+
import { PromiseOrValue } from '../jsutils/PromiseOrValue';
3+
import { Path, addPath, pathToArray } from '../jsutils/Path';
4+
25
import { GraphQLError, locatedError } from '../error';
3-
import { GraphQLSchema } from '../type/schema';
4-
import {
5-
GraphQLField,
6-
GraphQLFieldResolver,
7-
ResponsePath,
8-
GraphQLObjectType,
9-
GraphQLResolveInfo,
10-
} from '../type/definition';
116
import {
127
DirectiveNode,
138
DocumentNode,
@@ -17,7 +12,14 @@ import {
1712
InlineFragmentNode,
1813
FragmentDefinitionNode,
1914
} from '../language/ast';
20-
import { PromiseOrValue } from '../jsutils/PromiseOrValue';
15+
import { GraphQLSchema } from '../type/schema';
16+
import {
17+
GraphQLField,
18+
GraphQLFieldResolver,
19+
GraphQLResolveInfo,
20+
GraphQLTypeResolver,
21+
GraphQLObjectType,
22+
} from '../type/definition';
2123

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

5457
export type ExecutionArgs = {
@@ -59,6 +62,7 @@ export type ExecutionArgs = {
5962
variableValues?: Maybe<{ [key: string]: any }>;
6063
operationName?: Maybe<string>;
6164
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
65+
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
6266
};
6367

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

89-
/**
90-
* Given a ResponsePath (found in the `path` entry in the information provided
91-
* as the last argument to a field resolver), return an Array of the path keys.
92-
*/
93-
export function responsePathAsArray(
94-
path: ResponsePath,
95-
): ReadonlyArray<string | number>;
96-
97-
/**
98-
* Given a ResponsePath and a key, return a new ResponsePath containing the
99-
* new key.
100-
101-
*/
102-
export function addPath(
103-
prev: ResponsePath | undefined,
104-
key: string | number,
105-
): { prev: ResponsePath | undefined; key: string | number };
106-
10794
/**
10895
* Essential assertions before executing to provide developer feedback for
10996
* improper use of the GraphQL library.
@@ -128,6 +115,7 @@ export function buildExecutionContext(
128115
rawVariableValues: Maybe<{ [key: string]: any }>,
129116
operationName: Maybe<string>,
130117
fieldResolver: Maybe<GraphQLFieldResolver<any, any>>,
118+
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
131119
): ReadonlyArray<GraphQLError> | ExecutionContext;
132120

133121
/**
@@ -151,11 +139,12 @@ export function buildResolveInfo(
151139
fieldDef: GraphQLField<any, any>,
152140
fieldNodes: ReadonlyArray<FieldNode>,
153141
parentType: GraphQLObjectType,
154-
path: ResponsePath,
142+
path: Path,
155143
): GraphQLResolveInfo;
156144

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

157+
/**
158+
* If a resolveType function is not given, then a default resolve behavior is
159+
* used which attempts two strategies:
160+
*
161+
* First, See if the provided value has a `__typename` field defined, if so, use
162+
* that value as name of the resolved type.
163+
*
164+
* Otherwise, test each possible type for the abstract type by calling
165+
* isTypeOf for the object being coerced, returning the first type that matches.
166+
*/
167+
export const defaultTypeResolver: GraphQLTypeResolver<any, any>;
168+
168169
/**
169170
* If a resolve function is not given, then a default resolve behavior is used
170171
* which takes the property of the source object of the same name as the field

tstypes/execution/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
export { pathToArray as responsePathAsArray } from '../jsutils/Path';
2+
13
export {
24
execute,
35
defaultFieldResolver,
4-
responsePathAsArray,
6+
defaultTypeResolver,
57
ExecutionArgs,
68
ExecutionResult,
79
} from './execute';

tstypes/execution/values.d.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import Maybe from '../tsutils/Maybe';
22
import { GraphQLError } from '../error/GraphQLError';
3-
import {
4-
GraphQLInputType,
5-
GraphQLField,
6-
GraphQLArgument,
7-
} from '../type/definition';
8-
import { GraphQLDirective } from '../type/directives';
9-
import { GraphQLSchema } from '../type/schema';
103
import {
114
FieldNode,
125
DirectiveNode,
136
VariableDefinitionNode,
147
} from '../language/ast';
158

16-
interface CoercedVariableValues {
17-
errors: ReadonlyArray<GraphQLError> | undefined;
18-
coerced: { [key: string]: any } | undefined;
19-
}
9+
import { GraphQLDirective } from '../type/directives';
10+
import { GraphQLSchema } from '../type/schema';
11+
import {
12+
GraphQLInputType,
13+
GraphQLField,
14+
GraphQLArgument,
15+
} from '../type/definition';
16+
17+
type CoercedVariableValues =
18+
| { errors: ReadonlyArray<GraphQLError>; coerced?: never }
19+
| { errors?: never; coerced: { [key: string]: any } };
2020

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

3637
/**

tstypes/jsutils/Path.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export type Path = {
2+
prev: Path | void;
3+
key: string | number;
4+
};
5+
6+
/**
7+
* Given a Path and a key, return a new Path containing the new key.
8+
*/
9+
export function addPath(prev: Path | undefined, key: string | number): Path;
10+
11+
/**
12+
* Given a Path, return an Array of the path keys.
13+
*/
14+
export function pathToArray(path: Path): Array<string | number>;

0 commit comments

Comments
 (0)