From a9fcb44713dbb78b444302e827f5c264cb2911d9 Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Wed, 27 Sep 2023 07:20:41 -0700 Subject: [PATCH] fix: add support for TypedDocumentNode to be passed to WS methods (#585) --- src/graphql-ws.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/graphql-ws.ts b/src/graphql-ws.ts index b2b19d3c4..fbb14a366 100644 --- a/src/graphql-ws.ts +++ b/src/graphql-ws.ts @@ -2,6 +2,7 @@ import { resolveRequestDocument } from './resolveRequestDocument.js' import type { RequestDocument, Variables } from './types.js' import { ClientError } from './types.js' +import { TypedDocumentNode } from '@graphql-typed-document-node/core' // import type WebSocket from 'ws' const CONNECTION_INIT = `connection_init` @@ -159,7 +160,7 @@ export class GraphQLWebSocketClient { case ERROR: { subscriber.error && subscriber.error( - new ClientError({ errors: message.payload, status: 200 }, { query, variables }) + new ClientError({ errors: message.payload, status: 200 }, { query, variables }), ) return } @@ -183,7 +184,7 @@ export class GraphQLWebSocketClient { query: string, operationName: string | undefined, subscriber: GraphQLSubscriber, - variables?: V + variables?: V, ): UnsubscribeCallback { const subscriptionId = (this.socketState.lastRequestId++).toString() this.socketState.subscriptions[subscriptionId] = { query, variables, subscriber } @@ -196,7 +197,7 @@ export class GraphQLWebSocketClient { rawRequest( query: string, - variables?: V + variables?: V, ): Promise<{ data: T; extensions?: E }> { return new Promise<{ data: T; extensions?: E; headers?: Headers; status?: number }>((resolve, reject) => { let result: { data: T; extensions?: E } @@ -207,12 +208,15 @@ export class GraphQLWebSocketClient { error: reject, complete: () => resolve(result), }, - variables + variables, ) }) } - request(document: RequestDocument, variables?: V): Promise { + request( + document: RequestDocument | TypedDocumentNode, + variables?: V, + ): Promise { return new Promise((resolve, reject) => { let result: T this.subscribe( @@ -222,15 +226,15 @@ export class GraphQLWebSocketClient { error: reject, complete: () => resolve(result), }, - variables + variables, ) }) } subscribe( - document: RequestDocument, + document: RequestDocument | TypedDocumentNode, subscriber: GraphQLSubscriber, - variables?: V + variables?: V, ): UnsubscribeCallback { const { query, operationName } = resolveRequestDocument(document) return this.makeSubscribe(query, operationName, subscriber, variables) @@ -239,7 +243,7 @@ export class GraphQLWebSocketClient { rawSubscribe( query: string, subscriber: GraphQLSubscriber, - variables?: V + variables?: V, ): UnsubscribeCallback { return this.makeSubscribe(query, undefined, subscriber, variables) }