execute.d.ts 6.5 KB

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