UNPKG

graphql-sse

Version:

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

49 lines (48 loc) 1.95 kB
/// <reference types="node" /> import type { Middleware, ParameterizedContext, DefaultState, DefaultContext } from 'koa'; import type { IncomingMessage } from 'http'; import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler.mjs'; /** * Handler options when using the koa adapter. * * @category Server/koa */ export type HandlerOptions<Context extends OperationContext = undefined, KoaState = DefaultState, KoaContext = DefaultContext> = RawHandlerOptions<IncomingMessage, ParameterizedContext<KoaState, KoaContext>, Context>; /** * The ready-to-use handler for [Koa](https://expressjs.com). * * 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. * * ```js * import Koa from 'koa'; // yarn add koa * import mount from 'koa-mount'; // yarn add koa-mount * import { createHandler } from 'graphql-sse/lib/use/koa'; * import { schema } from './my-graphql/index.mjs'; * * const app = new Koa(); * app.use( * mount('/graphql/stream', async (ctx, next) => { * try { * await handler(ctx, next); * } catch (err) { * console.error(err); * ctx.response.status = 500; * ctx.response.message = 'Internal Server Error'; * } * }), * ); * * app.listen({ port: 4000 }); * console.log('Listening to port 4000'); * ``` * * @category Server/koa */ export declare function createHandler<Context extends OperationContext = undefined, KoaState = DefaultState, KoaContext = DefaultContext>(options: HandlerOptions<Context, KoaState, KoaContext>): Middleware<KoaState, KoaContext>;