import type { Instrumentation as EnvelopInstrumentation, Plugin as EnvelopPlugin, OnExecuteHook, OnSubscribeHook, PromiseOrValue, RegisterContextErrorHandler, SetSchemaFn } from '@envelop/core'; import type { ExecutionResult } from '@graphql-tools/utils'; import type { MaybePromise } from '@whatwg-node/promise-helpers'; import type { ServerAdapterInitialContext, Instrumentation as ServerAdapterInstrumentation, ServerAdapterPlugin } from '@whatwg-node/server'; import type { YogaServer } from '../server.cjs'; import type { FetchAPI, GraphQLHTTPExtensions, GraphQLParams, MaybeArray, YogaInitialContext } from '../types.cjs'; export type Plugin = {}, TServerContext extends Record = {}, TUserContext = {}> = EnvelopPlugin & ServerAdapterPlugin & { /** * onExecute hook that is invoked before the execute function is invoked. */ onExecute?: OnExecuteHook; /** * onSubscribe hook that is invoked before the subscribe function is called. * Return a OnSubscribeHookResult for hooking into phase after the subscribe function has been called. */ onSubscribe?: OnSubscribeHook; /** * Invoked when a plugin is initialized. You can use this hook to add other plugin you may depend on. */ onPluginInit?: OnPluginInitHook; } & { /** * A Tracer instance that will wrap each phases of the request pipeline. * This should be used primarly as an observability tool (for monitoring, tracing, etc...). */ instrumentation?: Instrumentation; /** * This hook is invoked at Yoga Server initialization, before it starts. * Here you can setup long running resources (like monitoring or caching clients) * or customize the Yoga instance. */ onYogaInit?: OnYogaInitHook; /** * This hook is invoked for any incoming GraphQL HTTP request and is invoked before attempting * to parse the GraphQL parameters. Here you can manipulate the request, set a custom request * parser or apply security measures such as checking for access tokens etc. */ onRequestParse?: OnRequestParseHook; /** * This hook is invoked for an incoming GraphQL request after the GraphQL parameters * (query, variables, extensions and operationName) have been ATTEMPTED to be parsed. * * Within this hook you can manipulate and customize the parameters or even implement a whole * new way of parsing the parameters. * * In addition to that you could also short-circuit and skip the GraphQL execution. */ onParams?: OnParamsHook; /** * This hook is invoked for each result produced for GraphQL operation, before it is processed * to be sent to client. * * In particular, if a request contains batched operations, this hook is called once of each * operation. * * Here, you can modify the result, to add monitoring or instrumentation extensions for example. */ onExecutionResult?: OnExecutionResultHook; /** * This hook is invoked after a GraphQL request has been processed and before the response is * forwarded to the client. Here you can customize what transport/response processor format * should be used for sending the result over the wire. */ onResultProcess?: OnResultProcess; }; export type Instrumentation> = EnvelopInstrumentation & ServerAdapterInstrumentation & { operation?: (payload: { context: TContext; request: Request; }, wrapped: () => PromiseOrValue) => PromiseOrValue; requestParse?: (payload: { request: Request; }, wrapped: () => MaybePromise) => MaybePromise; resultProcess?: (payload: { request: Request; }, wrapped: () => MaybePromise) => MaybePromise; }; export type OnYogaInitHook> = (payload: OnYogaInitEventPayload) => void; export type OnYogaInitEventPayload> = { yoga: YogaServer; }; export type OnRequestParseHook = (payload: OnRequestParseEventPayload) => PromiseOrValue; export type RequestParser = (request: Request) => PromiseOrValue; export interface OnRequestParseEventPayload { request: Request; url: URL; requestParser: RequestParser | undefined; serverContext: TServerContext & ServerAdapterInitialContext; setRequestParser: (parser: RequestParser) => void; fetchAPI: FetchAPI; endResponse: (response: Response) => void; } export type OnRequestParseHookResult = { onRequestParseDone?: OnRequestParseDoneHook; }; export type OnRequestParseDoneHook = (payload: OnRequestParseDoneEventPayload) => PromiseOrValue; export interface OnRequestParseDoneEventPayload { requestParserResult: GraphQLParams | GraphQLParams[]; setRequestParserResult: (params: GraphQLParams | GraphQLParams[]) => void; } export type OnParamsHook = (payload: OnParamsEventPayload) => PromiseOrValue; export interface OnParamsEventPayload> { request: Request; params: GraphQLParams; setParams: (params: GraphQLParams) => void; paramsHandler: ParamsHandler; setParamsHandler: (handler: ParamsHandler) => void; setResult: (result: ExecutionResult | AsyncIterable) => void; fetchAPI: FetchAPI; context: TServerContext; } export interface ParamsHandlerPayload { request: Request; params: GraphQLParams; context: TServerContext & ServerAdapterInitialContext & YogaInitialContext; } export type ParamsHandler = (payload: ParamsHandlerPayload) => PromiseOrValue>; export type OnResultProcess = (payload: OnResultProcessEventPayload) => PromiseOrValue; export type ExecutionResultWithSerializer = ExecutionResult & { stringify?: (result: ExecutionResult) => string; }; export type OnExecutionResultHook = (payload: OnExecutionResultEventPayload) => PromiseOrValue; export interface OnExecutionResultEventPayload { request: Request; result: ExecutionResultWithSerializer | AsyncIterable | undefined; setResult(result: ExecutionResultWithSerializer | AsyncIterable): void; context: TServerContext & ServerAdapterInitialContext & YogaInitialContext; } export type ResultProcessorInput = MaybeArray | AsyncIterable>; export type ResultProcessor = (result: ResultProcessorInput, fetchAPI: FetchAPI, acceptedMediaType: string) => PromiseOrValue; export interface OnResultProcessEventPayload { request: Request; result: ResultProcessorInput; setResult(result: ResultProcessorInput): void; resultProcessor?: ResultProcessor; acceptableMediaTypes: string[]; setResultProcessor(resultProcessor: ResultProcessor, acceptedMediaType: string): void; serverContext: TServerContext & ServerAdapterInitialContext; } /** * Payload forwarded to the onPluginInit hook. */ export type OnPluginInitEventPayload> = { /** * Register a new plugin. */ addPlugin: (newPlugin: Plugin) => void; /** * A list of all currently active plugins. */ plugins: Plugin[]; /** * Set the GraphQL schema. */ setSchema: SetSchemaFn; /** * Register an error handler used for context creation. */ registerContextErrorHandler: RegisterContextErrorHandler; }; /** * Invoked when a plugin is initialized. */ export type OnPluginInitHook> = (options: OnPluginInitEventPayload) => void;