execute.d.ts 6.7 KB

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