UNPKG

@graphql-tools/graphql

Version:
201 lines (200 loc) 8.98 kB
import type { Maybe } from '../jsutils/Maybe.cjs'; import type { ObjMap } from '../jsutils/ObjMap.cjs'; import type { Path } from '../jsutils/Path.cjs'; import type { PromiseOrValue } from '../jsutils/PromiseOrValue.cjs'; import type { GraphQLFormattedError } from '../error/GraphQLError.cjs'; import { GraphQLError } from '../error/GraphQLError.cjs'; import type { DocumentNode, FieldNode, FragmentDefinitionNode, OperationDefinitionNode } from '../language/ast.cjs'; import type { GraphQLField, GraphQLFieldResolver, GraphQLObjectType, GraphQLResolveInfo, GraphQLTypeResolver } from '../type/definition.cjs'; import type { GraphQLSchema } from '../type/schema.cjs'; /** * Terminology * * "Definitions" are the generic name for top-level statements in the document. * Examples of this include: * 1) Operations (such as a query) * 2) Fragments * * "Operations" are a generic name for requests in the document. * Examples of this include: * 1) query, * 2) mutation * * "Selections" are the definitions that can appear legally and at * single level of the query. These include: * 1) field references e.g `a` * 2) fragment "spreads" e.g. `...c` * 3) inline fragment "spreads" e.g. `...on Type { a }` */ /** * Data that must be available at all points during query execution. * * Namely, schema of the type system that is currently executing, * and the fragments defined in the query document */ export interface ExecutionContext { schema: GraphQLSchema; fragments: ObjMap<FragmentDefinitionNode>; rootValue: unknown; contextValue: unknown; operation: OperationDefinitionNode; variableValues: { [variable: string]: unknown; }; fieldResolver: GraphQLFieldResolver<any, any>; typeResolver: GraphQLTypeResolver<any, any>; subscribeFieldResolver: GraphQLFieldResolver<any, any>; errors: Array<GraphQLError>; } /** * The result of GraphQL execution. * * - `errors` is included when any errors occurred as a non-empty array. * - `data` is the result of a successful execution of the query. * - `extensions` is reserved for adding non-standard properties. */ export interface ExecutionResult<TData = ObjMap<unknown>, TExtensions = ObjMap<unknown>> { errors?: ReadonlyArray<GraphQLError>; data?: TData | null; extensions?: TExtensions; } export interface FormattedExecutionResult<TData = ObjMap<unknown>, TExtensions = ObjMap<unknown>> { errors?: ReadonlyArray<GraphQLFormattedError>; data?: TData | null; extensions?: TExtensions; } export interface ExecutionArgs { schema: GraphQLSchema; document: DocumentNode; rootValue?: unknown; contextValue?: unknown; variableValues?: Maybe<{ readonly [variable: string]: unknown; }>; operationName?: Maybe<string>; fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>; typeResolver?: Maybe<GraphQLTypeResolver<any, any>>; subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>; } /** * Implements the "Executing requests" section of the GraphQL specification. * * Returns either a synchronous ExecutionResult (if all encountered resolvers * are synchronous), or a Promise of an ExecutionResult that will eventually be * resolved and never rejected. * * If the arguments to this function do not result in a legal execution context, * a GraphQLError will be thrown immediately explaining the invalid input. */ export declare function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult>; /** * Also implements the "Executing requests" section of the GraphQL specification. * However, it guarantees to complete synchronously (or throw an error) assuming * that all field resolvers are also synchronous. */ export declare function executeSync(args: ExecutionArgs): ExecutionResult; /** * Essential assertions before executing to provide developer feedback for * improper use of the GraphQL library. * * @internal */ export declare function assertValidExecutionArguments(schema: GraphQLSchema, document: DocumentNode, rawVariableValues: Maybe<{ readonly [variable: string]: unknown; }>): void; /** * Constructs a ExecutionContext object from the arguments passed to * execute, which we will pass throughout the other execution methods. * * Throws a GraphQLError if a valid execution context cannot be created. * * TODO: consider no longer exporting this function * @internal */ export declare function buildExecutionContext(args: ExecutionArgs): ReadonlyArray<GraphQLError> | ExecutionContext; /** * TODO: consider no longer exporting this function * @internal */ export declare function buildResolveInfo(exeContext: ExecutionContext, fieldDef: GraphQLField<unknown, unknown>, fieldNodes: ReadonlyArray<FieldNode>, parentType: GraphQLObjectType, path: Path): GraphQLResolveInfo; /** * If a resolveType function is not given, then a default resolve behavior is * used which attempts two strategies: * * First, See if the provided value has a `__typename` field defined, if so, use * that value as name of the resolved type. * * Otherwise, test each possible type for the abstract type by calling * isTypeOf for the object being coerced, returning the first type that matches. */ export declare const defaultTypeResolver: GraphQLTypeResolver<unknown, unknown>; /** * If a resolve function is not given, then a default resolve behavior is used * which takes the property of the source object of the same name as the field * and returns it as the result, or if it's a function, returns the result * of calling that function while passing along args and context value. */ export declare const defaultFieldResolver: GraphQLFieldResolver<unknown, unknown>; /** * Implements the "Subscribe" algorithm described in the GraphQL specification. * * Returns a Promise which resolves to either an AsyncIterator (if successful) * or an ExecutionResult (error). The promise will be rejected if the schema or * other arguments to this function are invalid, or if the resolved event stream * is not an async iterable. * * If the client-provided arguments to this function do not result in a * compliant subscription, a GraphQL Response (ExecutionResult) with * descriptive errors and no data will be returned. * * If the source stream could not be created due to faulty subscription * resolver logic or underlying systems, the promise will resolve to a single * ExecutionResult containing `errors` and no `data`. * * If the operation succeeded, the promise resolves to an AsyncIterator, which * yields a stream of ExecutionResults representing the response stream. * * Accepts either an object with named arguments, or individual arguments. */ export declare function subscribe(args: ExecutionArgs): PromiseOrValue<AsyncGenerator<ExecutionResult, void, void> | ExecutionResult>; /** * Implements the "CreateSourceEventStream" algorithm described in the * GraphQL specification, resolving the subscription source event stream. * * Returns a Promise which resolves to either an AsyncIterable (if successful) * or an ExecutionResult (error). The promise will be rejected if the schema or * other arguments to this function are invalid, or if the resolved event stream * is not an async iterable. * * If the client-provided arguments to this function do not result in a * compliant subscription, a GraphQL Response (ExecutionResult) with * descriptive errors and no data will be returned. * * If the the source stream could not be created due to faulty subscription * resolver logic or underlying systems, the promise will resolve to a single * ExecutionResult containing `errors` and no `data`. * * If the operation succeeded, the promise resolves to the AsyncIterable for the * event stream returned by the resolver. * * A Source Event Stream represents a sequence of events, each of which triggers * a GraphQL execution for that event. * * This may be useful when hosting the stateful subscription service in a * different process or machine than the stateless GraphQL execution engine, * or otherwise separating these two steps. For more on this, see the * "Supporting Subscriptions at Scale" information in the GraphQL specification. */ export declare function createSourceEventStream(args: ExecutionArgs): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult>; /** * This method looks up the field on the given type definition. * It has special casing for the three introspection fields, * __schema, __type and __typename. __typename is special because * it can always be queried as a field, even in situations where no * other fields are allowed, like on a Union. __schema and __type * could get automatically added to the query type, but that would * require mutating type definitions, which would cause issues. * * @internal */ export declare function getFieldDef(schema: GraphQLSchema, parentType: GraphQLObjectType, fieldNode: FieldNode): Maybe<GraphQLField<unknown, unknown>>;