UNPKG

@fanoutio/subscriptions-transport-ws-over-http

Version:
99 lines (98 loc) 4.33 kB
/// <reference types="node" /> import http, { IncomingMessage, ServerResponse } from 'http'; import https from 'https'; import { IGripConfig, Publisher, WebSocketContext } from "@fanoutio/grip"; import { ServeGrip } from '@fanoutio/serve-grip'; import { ExecutionResult, GraphQLSchema, DocumentNode, GraphQLFieldResolver, ValidationRule } from 'graphql'; import { IPersistence } from "../persistence/persistence"; export declare type ExecutionIterator = AsyncIterator<ExecutionResult>; export declare type IFormatResultFunction = (value: ExecutionResult, params: ExecutionParams) => ExecutionResult; export declare type IFormatErrorFunction = (e: Error, params: ExecutionParams) => Error; export interface ExecutionParams<TContext = any> { query: string | DocumentNode; variables: { [key: string]: any; }; operationName: string; context: TContext; formatResponse?: IFormatResultFunction; formatError?: IFormatErrorFunction; schema?: GraphQLSchema; } export declare type ConnectionContext = { initPromise?: Promise<any>; wsId: string; wsContext?: WebSocketContext; request?: IncomingMessage; operations: { [opId: string]: ExecutionIterator; }; }; export interface OperationMessagePayload { [key: string]: any; query?: string; variables?: { [key: string]: any; }; operationName?: string; } export interface OperationMessage { payload?: OperationMessagePayload; id?: string; type: string; } export declare type ExecuteFunction = (schema: GraphQLSchema, document: DocumentNode, rootValue?: any, contextValue?: any, variableValues?: { [key: string]: any; }, operationName?: string, fieldResolver?: GraphQLFieldResolver<any, any>) => ExecutionResult | Promise<ExecutionResult> | AsyncIterator<ExecutionResult>; export declare type SubscribeFunction = (schema: GraphQLSchema, document: DocumentNode, rootValue?: any, contextValue?: any, variableValues?: { [key: string]: any; }, operationName?: string, fieldResolver?: GraphQLFieldResolver<any, any>, subscribeFieldResolver?: GraphQLFieldResolver<any, any>) => AsyncIterator<ExecutionResult> | Promise<AsyncIterator<ExecutionResult> | ExecutionResult>; export declare type IOnOperationHandler = (message: OperationMessage, baseParams: ExecutionParams, wsContextId: string | undefined) => Promise<ExecutionParams>; export declare type IOnOperationCompleteHandler = (wsContextId: string | undefined, opId: string) => void; export declare type IOnConnectHandler = (payload: OperationMessagePayload | undefined, wsContextId: string | undefined, connectionContext: ConnectionContext) => Promise<object | boolean>; export declare type IOnDisconnectHandler = (wsContextId: string | undefined, connectionContext: ConnectionContext) => void; export interface ServerOptions { rootValue?: any; schema?: GraphQLSchema; execute?: ExecuteFunction; subscribe?: SubscribeFunction; validationRules?: ValidationRule[]; onOperation?: IOnOperationHandler; onOperationComplete?: IOnOperationCompleteHandler; onConnect?: IOnConnectHandler; onDisconnect?: IOnDisconnectHandler; keepAlive?: number; persistence?: IPersistence; grip: string | IGripConfig | IGripConfig[] | Publisher; gripPrefix?: string; } export declare class SubscriptionServer { private readonly server?; private execute?; private subscribe?; private schema?; private rootValue?; private readonly keepAlive?; private readonly specifiedRules; readonly serveGrip: ServeGrip; private readonly onOperation?; private readonly onOperationComplete?; private readonly onConnect?; private readonly onDisconnect?; private readonly readyPromise; private readonly persistence; static create(options: ServerOptions, server?: http.Server | https.Server): SubscriptionServer; constructor(options: ServerOptions, server?: http.Server | https.Server); requestHandler(req: IncomingMessage, res: ServerResponse): Promise<boolean>; private prep; ready(): Promise<void>; private _connectionContexts; private getConnectionContext; private loadExecutor; private unsubscribe; private onClose; private onMessage; private sendKeepAlive; private sendMessage; private sendError; }