UNPKG

4.06 kBTypeScriptView Raw
1/// <reference types="node" />
2import { EventEmitter } from 'events';
3import { GraphQLResolveInfo } from 'graphql';
4import { IDebugger } from '@cortexql/core';
5import { Request } from 'express';
6import * as WebSocket from 'ws';
7import { OperationMessage, ExecutionParams } from 'subscriptions-transport-ws';
8import { GraphQLSchema, DocumentNode, ExecutionResult } from 'graphql';
9export interface ContextExecute {
10 (document: DocumentNode, variableValues?: {
11 [key: string]: any;
12 }, operationName?: string): Promise<ExecutionResult>;
13}
14export interface ContextSubscribe {
15 (document: DocumentNode, variableValues?: {
16 [key: string]: any;
17 }, operationName?: string): Promise<ExecutionResult | AsyncIterator<ExecutionResult>>;
18}
19export interface ContextParams {
20 [key: string]: any;
21}
22export declare function createContextId(context: BaseContext): any;
23export declare function createExecutionContextId(context: BaseContext, info?: GraphQLResolveInfo): any;
24export declare function createContextNamespace(context: BaseContext, info?: GraphQLResolveInfo, id?: any): string;
25export declare class BaseContext {
26 params: ContextParams;
27 operationName: string | undefined;
28 operationType: string | undefined;
29 id: any;
30 schema: GraphQLSchema;
31 event: EventEmitter;
32 executionEvent?: EventEmitter;
33 execute: ContextExecute;
34 subscribe: ContextSubscribe;
35 context?: this;
36 completed: boolean;
37 completionError?: Error;
38 completionReason?: string;
39 isExecution: false;
40 destroy(handler: (...args: any[]) => void): EventEmitter;
41 on(event: string, handler: (...args: any[]) => void): EventEmitter;
42 once(event: string, handler: (...args: any[]) => void): EventEmitter;
43 removeListener(event: string, handler: (...args: any[]) => void): EventEmitter;
44 removeAllListeners(event: string): EventEmitter;
45 emit(event: string, ...args: any[]): boolean;
46 constructor(params: ContextParams, operationName?: string | undefined, operationType?: string | undefined, id?: any);
47 debug: IDebugger;
48 execution(info?: GraphQLResolveInfo): Promise<ExecutionContext<this>>;
49 end(): void;
50 end(error: Error | null, reason?: string): void;
51 end(handler: (error?: Error | null, reason?: string) => void): Promise<void>;
52}
53export declare class BaseExecutionContext<T extends BaseContext> {
54 context: T;
55 info: GraphQLResolveInfo | undefined;
56 executionEvent: EventEmitter;
57 isExecution: boolean;
58 id: any;
59 debug: IDebugger;
60 completed: boolean;
61 completionError?: Error;
62 completionReason?: string;
63 constructor(context: T, info?: GraphQLResolveInfo | undefined);
64 destroy(handler: (...args: any[]) => void): EventEmitter;
65 on(event: string, handler: (...args: any[]) => void): EventEmitter;
66 once(event: string, handler: (...args: any[]) => void): EventEmitter;
67 removeListener(event: string, handler: (...args: any[]) => void): EventEmitter;
68 removeAllListeners(event: string): EventEmitter;
69 emit(event: string, ...args: any[]): boolean;
70 end(): void;
71 end(error: Error | null, reason?: string): void;
72 end(handler: (error?: Error | null, reason?: string) => void): Promise<void>;
73}
74export declare type ExecutionContext<T extends BaseContext> = T & BaseExecutionContext<T>;
75export declare function createContext(params: ContextParams, operationName?: string, operationType?: string, executionId?: any): Promise<BaseContext>;
76export declare function createExecutionContext<T extends BaseContext>(context: T, info?: GraphQLResolveInfo): Promise<ExecutionContext<T>>;
77export declare function getOperationFromQuery(query: string): {
78 operationName: string;
79 operationType: string;
80} | {
81 operationName: undefined;
82 operationType: undefined;
83};
84export declare function createContextFromExpressRequest(req: Request, params?: ContextParams): Promise<BaseContext>;
85export declare function createContextFromWebSocketTransport(webSocket: WebSocket, message: OperationMessage, params: ExecutionParams): Promise<BaseContext>;