execute.d.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import type { Maybe } from '../jsutils/Maybe';
  2. import type { ObjMap } from '../jsutils/ObjMap';
  3. import type { Path } from '../jsutils/Path';
  4. import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
  5. import type { GraphQLFormattedError } from '../error/GraphQLError';
  6. import { GraphQLError } from '../error/GraphQLError';
  7. import type {
  8. DocumentNode,
  9. FieldNode,
  10. FragmentDefinitionNode,
  11. OperationDefinitionNode,
  12. } from '../language/ast';
  13. import type {
  14. GraphQLField,
  15. GraphQLFieldResolver,
  16. GraphQLObjectType,
  17. GraphQLResolveInfo,
  18. GraphQLTypeResolver,
  19. } from '../type/definition';
  20. import 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. */
  46. export 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. */
  67. export interface ExecutionResult<
  68. TData = ObjMap<unknown>,
  69. TExtensions = ObjMap<unknown>,
  70. > {
  71. errors?: ReadonlyArray<GraphQLError>;
  72. data?: TData | null;
  73. extensions?: TExtensions;
  74. }
  75. export interface FormattedExecutionResult<
  76. TData = ObjMap<unknown>,
  77. TExtensions = ObjMap<unknown>,
  78. > {
  79. errors?: ReadonlyArray<GraphQLFormattedError>;
  80. data?: TData | null;
  81. extensions?: TExtensions;
  82. }
  83. export 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. */
  106. export 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. */
  114. export 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. */
  121. export 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. */
  136. export declare function buildExecutionContext(
  137. args: ExecutionArgs,
  138. ): ReadonlyArray<GraphQLError> | ExecutionContext;
  139. /**
  140. * @internal
  141. */
  142. export 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. */
  159. export 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. */
  166. export 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. */
  181. export declare function getFieldDef(
  182. schema: GraphQLSchema,
  183. parentType: GraphQLObjectType,
  184. fieldNode: FieldNode,
  185. ): Maybe<GraphQLField<unknown, unknown>>;