UNPKG

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