UNPKG

6.54 kBTypeScriptView Raw
1import Maybe from '../tsutils/Maybe';
2import { PromiseOrValue } from '../jsutils/PromiseOrValue';
3import { Path, addPath, pathToArray } from '../jsutils/Path';
4
5import { GraphQLError } from '../error/GraphQLError';
6import { locatedError } from '../error/locatedError';
7
8import {
9 DirectiveNode,
10 DocumentNode,
11 OperationDefinitionNode,
12 SelectionSetNode,
13 FieldNode,
14 InlineFragmentNode,
15 FragmentDefinitionNode,
16} from '../language/ast';
17import { GraphQLSchema } from '../type/schema';
18import {
19 GraphQLField,
20 GraphQLFieldResolver,
21 GraphQLResolveInfo,
22 GraphQLTypeResolver,
23 GraphQLObjectType,
24} from '../type/definition';
25
26/**
27 * Data that must be available at all points during query execution.
28 *
29 * Namely, schema of the type system that is currently executing,
30 * and the fragments defined in the query document
31 */
32export interface ExecutionContext {
33 schema: GraphQLSchema;
34 fragments: { [key: string]: FragmentDefinitionNode };
35 rootValue: any;
36 contextValue: any;
37 operation: OperationDefinitionNode;
38 variableValues: { [key: string]: any };
39 fieldResolver: GraphQLFieldResolver<any, any>;
40 errors: GraphQLError[];
41}
42
43export interface ExecutionResultDataDefault {
44 [key: string]: any;
45}
46
47/**
48 * The result of GraphQL execution.
49 *
50 * - `errors` is included when any errors occurred as a non-empty array.
51 * - `data` is the result of a successful execution of the query.
52 */
53// TS_SPECIFIC: TData and ExecutionResultDataDefault
54export interface ExecutionResult<TData = ExecutionResultDataDefault> {
55 errors?: ReadonlyArray<GraphQLError>;
56 data?: TData | null;
57}
58
59export type ExecutionArgs = {
60 schema: GraphQLSchema;
61 document: DocumentNode;
62 rootValue?: any;
63 contextValue?: any;
64 variableValues?: Maybe<{ [key: string]: any }>;
65 operationName?: Maybe<string>;
66 fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
67 typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
68};
69
70/**
71 * Implements the "Evaluating requests" section of the GraphQL specification.
72 *
73 * Returns either a synchronous ExecutionResult (if all encountered resolvers
74 * are synchronous), or a Promise of an ExecutionResult that will eventually be
75 * resolved and never rejected.
76 *
77 * If the arguments to this function do not result in a legal execution context,
78 * a GraphQLError will be thrown immediately explaining the invalid input.
79 *
80 * Accepts either an object with named arguments, or individual arguments.
81 */
82export function execute<TData = ExecutionResultDataDefault>(
83 args: ExecutionArgs,
84): PromiseOrValue<ExecutionResult<TData>>;
85export function execute<TData = ExecutionResultDataDefault>(
86 schema: GraphQLSchema,
87 document: DocumentNode,
88 rootValue?: any,
89 contextValue?: any,
90 variableValues?: Maybe<{ [key: string]: any }>,
91 operationName?: Maybe<string>,
92 fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
93 typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
94): PromiseOrValue<ExecutionResult<TData>>;
95
96/**
97 * Essential assertions before executing to provide developer feedback for
98 * improper use of the GraphQL library.
99 */
100export function assertValidExecutionArguments(
101 schema: GraphQLSchema,
102 document: DocumentNode,
103 rawVariableValues: Maybe<{ [key: string]: any }>,
104): void;
105
106/**
107 * Constructs a ExecutionContext object from the arguments passed to
108 * execute, which we will pass throughout the other execution methods.
109 *
110 * Throws a GraphQLError if a valid execution context cannot be created.
111 */
112export function buildExecutionContext(
113 schema: GraphQLSchema,
114 document: DocumentNode,
115 rootValue: any,
116 contextValue: any,
117 rawVariableValues: Maybe<{ [key: string]: any }>,
118 operationName: Maybe<string>,
119 fieldResolver: Maybe<GraphQLFieldResolver<any, any>>,
120 typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
121): ReadonlyArray<GraphQLError> | ExecutionContext;
122
123/**
124 * Given a selectionSet, adds all of the fields in that selection to
125 * the passed in map of fields, and returns it at the end.
126 *
127 * CollectFields requires the "runtime type" of an object. For a field which
128 * returns an Interface or Union type, the "runtime type" will be the actual
129 * Object type returned by that field.
130 */
131export function collectFields(
132 exeContext: ExecutionContext,
133 runtimeType: GraphQLObjectType,
134 selectionSet: SelectionSetNode,
135 fields: { [key: string]: Array<FieldNode> },
136 visitedFragmentNames: { [key: string]: boolean },
137): { [key: string]: Array<FieldNode> };
138
139export function buildResolveInfo(
140 exeContext: ExecutionContext,
141 fieldDef: GraphQLField<any, any>,
142 fieldNodes: ReadonlyArray<FieldNode>,
143 parentType: GraphQLObjectType,
144 path: Path,
145): GraphQLResolveInfo;
146
147// Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField`
148// function. Returns the result of resolveFn or the abrupt-return Error object.
149// TS_SPECIFIC: TSource
150export function resolveFieldValueOrError<TSource>(
151 exeContext: ExecutionContext,
152 fieldDef: GraphQLField<TSource, any>,
153 fieldNodes: ReadonlyArray<FieldNode>,
154 resolveFn: GraphQLFieldResolver<TSource, any>,
155 source: TSource,
156 info: GraphQLResolveInfo,
157): Error | any;
158
159/**
160 * If a resolveType function is not given, then a default resolve behavior is
161 * used which attempts two strategies:
162 *
163 * First, See if the provided value has a `__typename` field defined, if so, use
164 * that value as name of the resolved type.
165 *
166 * Otherwise, test each possible type for the abstract type by calling
167 * isTypeOf for the object being coerced, returning the first type that matches.
168 */
169export const defaultTypeResolver: GraphQLTypeResolver<any, any>;
170
171/**
172 * If a resolve function is not given, then a default resolve behavior is used
173 * which takes the property of the source object of the same name as the field
174 * and returns it as the result, or if it's a function, returns the result
175 * of calling that function while passing along args and context.
176 */
177export const defaultFieldResolver: GraphQLFieldResolver<any, any>;
178
179/**
180 * This method looks up the field on the given type defintion.
181 * It has special casing for the two introspection fields, __schema
182 * and __typename. __typename is special because it can always be
183 * queried as a field, even in situations where no other fields
184 * are allowed, like on a Union. __schema could get automatically
185 * added to the query type, but that would require mutating type
186 * definitions, which would cause issues.
187 */
188export function getFieldDef(
189 schema: GraphQLSchema,
190 parentType: GraphQLObjectType,
191 fieldName: string,
192): Maybe<GraphQLField<any, any>>;