UNPKG

graphql-sse

Version:

Zero-dependency, HTTP/1 safe, simple, GraphQL over Server-Sent Events Protocol server and client

49 lines (48 loc) 1.65 kB
/// <reference types="node" /> import type { IncomingMessage, ServerResponse } from 'http'; import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler'; /** * @category Server/http */ export interface RequestContext { res: ServerResponse; } /** * @category Server/http */ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<IncomingMessage, RequestContext, Context>; /** * The ready-to-use handler for Node's [http](https://nodejs.org/api/http.html). * * 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 http from 'http'; * import { createHandler } from 'graphql-sse/lib/use/http'; * import { schema } from './my-graphql'; * * const handler = createHandler({ schema }); * * const server = http.createServer(async (req, res) => { * try { * await handler(req, res); * } catch (err) { * console.error(err); * res.writeHead(500).end(); * } * }); * * server.listen(4000); * console.log('Listening to port 4000'); * ``` * * @category Server/http */ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (req: IncomingMessage, res: ServerResponse) => Promise<void>;