UNPKG

@graphql-yoga/plugin-persisted-operations

Version:

Persisted Operations plugin for GraphQL Yoga.

57 lines (56 loc) 2.4 kB
import { ASTNode, DocumentNode, GraphQLErrorExtensions, Source } from 'graphql'; import { GraphQLParams, Maybe, Plugin, PromiseOrValue, type OnParamsEventPayload } from 'graphql-yoga'; export interface GraphQLErrorOptions { nodes?: ReadonlyArray<ASTNode> | ASTNode | null | undefined; source?: Maybe<Source>; positions?: Maybe<ReadonlyArray<number>>; path?: Maybe<ReadonlyArray<string | number>>; originalError?: Maybe<Error & { readonly extensions?: unknown; }>; extensions?: Maybe<GraphQLErrorExtensions>; } export type ExtractPersistedOperationId = (params: GraphQLParams) => null | string; export declare const defaultExtractPersistedOperationId: ExtractPersistedOperationId; type AllowArbitraryOperationsHandler = (request: Request) => PromiseOrValue<boolean>; export type UsePersistedOperationsOptions = { /** * A function that fetches the persisted operation */ getPersistedOperation(key: string, request: Request): PromiseOrValue<DocumentNode | string | null>; /** * Whether to allow execution of arbitrary GraphQL operations aside from persisted operations. */ allowArbitraryOperations?: boolean | AllowArbitraryOperationsHandler; /** * The path to the persisted operation id */ extractPersistedOperationId?: ExtractPersistedOperationId; /** * Whether to skip validation of the persisted operation */ skipDocumentValidation?: boolean; /** * Custom errors to be thrown */ customErrors?: CustomPersistedQueryErrors; }; export type CustomErrorFactory = string | (GraphQLErrorOptions & { message: string; }) | ((payload: OnParamsEventPayload) => Error); export type CustomPersistedQueryErrors = { /** * Error to be thrown when the persisted operation is not found */ notFound?: CustomErrorFactory; /** * Error to be thrown when rejecting non-persisted operations */ persistedQueryOnly?: CustomErrorFactory; /** * Error to be thrown when the extraction of the persisted operation id failed */ keyNotFound?: CustomErrorFactory; }; export declare function usePersistedOperations<TPluginContext extends Record<string, any>>({ allowArbitraryOperations, extractPersistedOperationId, getPersistedOperation, skipDocumentValidation, customErrors, }: UsePersistedOperationsOptions): Plugin<TPluginContext>; export {};