Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export SubscriptionClient class #920

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,36 @@ export interface PreExecutionHookResponse<TError extends Error> {
document?: DocumentNode
errors?: TError[]
}

export interface SubscriptionClientConfig {
protocols: Array<string>,
reconnect: boolean,
maxReconnectAttempts: number,
serviceName: string,
connectionCallback: (params: any) => Promise<void>,
failedConnectionCallback: (params: any) => Promise<void>,
failedReconnectCallback: (params: any) => Promise<void>,
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<void>
startOperation (operationId: SubscriptionOperationId) : void

createSubscription (query: string, variables: any, publish: (props: any) => Promise<void>, context: MercuriusContext) : SubscriptionOperationId
startKeepAliveInterval() : void
stopKeepAliveInterval() : void
}
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -587,5 +588,6 @@ plugin.defaultErrorFormatter = defaultErrorFormatter
plugin.persistedQueryDefaults = persistedQueryDefaults
plugin.buildFederationSchema = buildFederationSchema
plugin.withFilter = withFilter
plugin.SubscriptionClient = SubscriptionClient

module.exports = plugin
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 36 additions & 1 deletion test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()