graphql-sse
Version:
Zero-dependency, HTTP/1 safe, simple, GraphQL over Server-Sent Events Protocol server and client
46 lines (45 loc) • 1.64 kB
TypeScript
import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
/**
* @category Server/fetch
*/
export interface RequestContext {
Response: typeof Response;
ReadableStream: typeof ReadableStream;
TextEncoder: typeof TextEncoder;
}
/**
* @category Server/fetch
*/
export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<Request, RequestContext, Context>;
/**
* The ready-to-use fetch handler. To be used with your favourite fetch
* framework, in a lambda function, or have deploy to the edge.
*
* Errors thrown from the provided options or callbacks (or even due to
* library misuse or potential bugs) will reject the handler or bubble to the
* returned iterator. They are considered internal errors and you should take care
* of them accordingly.
*
* For production environments, its recommended not to transmit the exact internal
* error details to the client, but instead report to an error logging tool or simply
* the console.
*
* ```ts
* import { createHandler } from 'graphql-sse/lib/use/fetch';
* import { schema } from './my-graphql';
*
* const handler = createHandler({ schema });
*
* export async function fetch(req: Request): Promise<Response> {
* try {
* return await handler(req);
* } catch (err) {
* console.error(err);
* return new Response(null, { status: 500 });
* }
* }
* ```
*
* @category Server/fetch
*/
export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>, reqCtx?: Partial<RequestContext>): (req: Request) => Promise<Response>;