UNPKG

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