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