From 91bbfa2d0eec7bb12c5b300efbb64d5f093b7699 Mon Sep 17 00:00:00 2001 From: Davide Fiorello Date: Mon, 21 Nov 2022 12:03:08 +0100 Subject: [PATCH] feat: export SubscriptionClient class --- index.d.ts | 33 +++++++++++++++++++++++++++++++++ index.js | 2 ++ package.json | 5 ++--- test/types/index.ts | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 5673216c..ae176658 100644 --- a/index.d.ts +++ b/index.d.ts @@ -833,3 +833,36 @@ export interface PreExecutionHookResponse { document?: DocumentNode errors?: TError[] } + +export interface SubscriptionClientConfig { + protocols: Array, + reconnect: boolean, + maxReconnectAttempts: number, + serviceName: string, + connectionCallback: (params: any) => Promise, + failedConnectionCallback: (params: any) => Promise, + failedReconnectCallback: (params: any) => Promise, + connectionInitPayload: any, + rewriteConnectionInitPayload: any, + keepAlive: boolean +} + +export type SubscriptionOperationId = string + +export class SubscriptionClient { + constructor(uri: string, config: SubscriptionClientConfig) + + connect(): void + close (tryReconnect: boolean, closedByUser?: boolean): void + getReconnectDelay() : number + reconnect() : void + unsubscribe (operationId: SubscriptionOperationId, forceUnsubscribe: boolean) : void + unsubscribeAll() : void + sendMessage (operationId: SubscriptionOperationId, type: any, payload : any, extensions: any) : void + handleMessage (message: any) : Promise + startOperation (operationId: SubscriptionOperationId) : void + + createSubscription (query: string, variables: any, publish: (props: any) => Promise, context: MercuriusContext) : SubscriptionOperationId + startKeepAliveInterval() : void + stopKeepAliveInterval() : void +} diff --git a/index.js b/index.js index 790a8350..d9e9b118 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,7 @@ const { initGateway } = require('./lib/gateway') const mq = require('mqemitter') const { PubSub, withFilter } = require('./lib/subscriber') const persistedQueryDefaults = require('./lib/persistedQueryDefaults') +const SubscriptionClient = require('./lib/subscription-client') const stringify = require('safe-stable-stringify') const { ErrorWithProps, @@ -587,5 +588,6 @@ plugin.defaultErrorFormatter = defaultErrorFormatter plugin.persistedQueryDefaults = persistedQueryDefaults plugin.buildFederationSchema = buildFederationSchema plugin.withFilter = withFilter +plugin.SubscriptionClient = SubscriptionClient module.exports = plugin diff --git a/package.json b/package.json index d9068f30..aa1d44d7 100644 --- a/package.json +++ b/package.json @@ -9,12 +9,11 @@ "unit": "tap test/*.js test/gateway/*.js test/internals/*.js", "cov": "tap --coverage-report=html -J test/*.js test/gateway/*.js", "lint": "npm run lint:standard && npm run lint:typescript", - "lint:fix": "standard --fix", + "lint:fix": "standard --fix && npm run lint:typescript -- --fix", "lint:standard": "standard | snazzy", "lint:typescript": "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin test/types/*.ts", "typescript": "tsd", - "test": "npm run lint && npm run unit && npm run typescript", - "test2": "npm run unit" + "test": "npm run lint && npm run unit && npm run typescript" }, "repository": { "type": "git", diff --git a/test/types/index.ts b/test/types/index.ts index 3e56828d..6539b273 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -6,12 +6,20 @@ import Fastify, { FastifyReply, FastifyRequest, FastifyInstance } from 'fastify' // eslint-disable-next-line no-unused-vars import { Readable } from 'stream' // eslint-disable-next-line no-unused-vars -import mercurius, { MercuriusOptions, IResolvers, MercuriusContext, MercuriusServiceMetadata, MercuriusPlugin } from '../..' +import mercurius, { + MercuriusOptions, + IResolvers, + MercuriusContext, + MercuriusServiceMetadata, + MercuriusPlugin, + SubscriptionClient +} from '../..' // eslint-disable-next-line no-unused-vars import { DocumentNode, ExecutionResult, GraphQLSchema, ValidationContext, ValidationRule } from 'graphql' import { makeExecutableSchema } from '@graphql-tools/schema' import { mapSchema } from '@graphql-tools/utils' import mq from 'mqemitter' +import { GRAPHQL_TRANSPORT_WS_PROTOCOL } from 'graphql-ws' const app = Fastify() @@ -916,3 +924,30 @@ expectError(() => { } }) }) + +const subscriptionClient = new SubscriptionClient('ws://localhost', { + protocols: [GRAPHQL_TRANSPORT_WS_PROTOCOL], + reconnect: false, + maxReconnectAttempts: 10, + serviceName: 'sample-name', + connectionCallback: async () => {}, + failedConnectionCallback: async () => {}, + failedReconnectCallback: async () => {}, + connectionInitPayload: {}, + rewriteConnectionInitPayload: {}, + keepAlive: true +}) + +subscriptionClient.connect() +subscriptionClient.reconnect() +subscriptionClient.close(true) +subscriptionClient.getReconnectDelay() +subscriptionClient.unsubscribeAll() +subscriptionClient.sendMessage('opId', 'test', {}, {}) +subscriptionClient.handleMessage({ foo: 'bar' }) +subscriptionClient.startOperation('opId') +subscriptionClient.createSubscription('query', {}, async () => { + +}, {} as MercuriusContext) +subscriptionClient.startKeepAliveInterval() +subscriptionClient.stopKeepAliveInterval()