server.d.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /// <reference types="node" />
  2. import * as WebSocket from 'ws';
  3. import { ExecutionResult, GraphQLSchema, DocumentNode, ValidationContext, GraphQLFieldResolver } from 'graphql';
  4. import { IncomingMessage } from 'http';
  5. export declare type ExecutionIterator = AsyncIterator<ExecutionResult>;
  6. export interface ExecutionParams<TContext = any> {
  7. query: string | DocumentNode;
  8. variables: {
  9. [key: string]: any;
  10. };
  11. operationName: string;
  12. context: TContext;
  13. formatResponse?: Function;
  14. formatError?: Function;
  15. callback?: Function;
  16. schema?: GraphQLSchema;
  17. }
  18. export declare type ConnectionContext = {
  19. initPromise?: Promise<any>;
  20. isLegacy: boolean;
  21. socket: WebSocket;
  22. request: IncomingMessage;
  23. operations: {
  24. [opId: string]: ExecutionIterator;
  25. };
  26. };
  27. export interface OperationMessagePayload {
  28. [key: string]: any;
  29. query?: string;
  30. variables?: {
  31. [key: string]: any;
  32. };
  33. operationName?: string;
  34. }
  35. export interface OperationMessage {
  36. payload?: OperationMessagePayload;
  37. id?: string;
  38. type: string;
  39. }
  40. export declare type ExecuteFunction = (schema: GraphQLSchema, document: DocumentNode, rootValue?: any, contextValue?: any, variableValues?: {
  41. [key: string]: any;
  42. }, operationName?: string, fieldResolver?: GraphQLFieldResolver<any, any>) => ExecutionResult | Promise<ExecutionResult> | AsyncIterator<ExecutionResult>;
  43. export declare type SubscribeFunction = (schema: GraphQLSchema, document: DocumentNode, rootValue?: any, contextValue?: any, variableValues?: {
  44. [key: string]: any;
  45. }, operationName?: string, fieldResolver?: GraphQLFieldResolver<any, any>, subscribeFieldResolver?: GraphQLFieldResolver<any, any>) => AsyncIterator<ExecutionResult> | Promise<AsyncIterator<ExecutionResult> | ExecutionResult>;
  46. export interface ServerOptions {
  47. rootValue?: any;
  48. schema?: GraphQLSchema;
  49. execute?: ExecuteFunction;
  50. subscribe?: SubscribeFunction;
  51. validationRules?: Array<(context: ValidationContext) => any> | ReadonlyArray<any>;
  52. onOperation?: Function;
  53. onOperationComplete?: Function;
  54. onConnect?: Function;
  55. onDisconnect?: Function;
  56. keepAlive?: number;
  57. }
  58. export declare class SubscriptionServer {
  59. private onOperation;
  60. private onOperationComplete;
  61. private onConnect;
  62. private onDisconnect;
  63. private wsServer;
  64. private execute;
  65. private subscribe;
  66. private schema;
  67. private rootValue;
  68. private keepAlive;
  69. private closeHandler;
  70. private specifiedRules;
  71. static create(options: ServerOptions, socketOptionsOrServer: WebSocket.ServerOptions | WebSocket.Server): SubscriptionServer;
  72. constructor(options: ServerOptions, socketOptionsOrServer: WebSocket.ServerOptions | WebSocket.Server);
  73. get server(): WebSocket.Server;
  74. close(): void;
  75. private loadExecutor;
  76. private unsubscribe;
  77. private onClose;
  78. private onMessage;
  79. private sendKeepAlive;
  80. private sendMessage;
  81. private sendError;
  82. }