Skip to content

Commit

Permalink
fix: add support for TypedDocumentNode to be passed to WS methods (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn authored Sep 27, 2023
1 parent e52e70d commit a9fcb44
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/graphql-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
}
Expand All @@ -183,7 +184,7 @@ export class GraphQLWebSocketClient {
query: string,
operationName: string | undefined,
subscriber: GraphQLSubscriber<T, E>,
variables?: V
variables?: V,
): UnsubscribeCallback {
const subscriptionId = (this.socketState.lastRequestId++).toString()
this.socketState.subscriptions[subscriptionId] = { query, variables, subscriber }
Expand All @@ -196,7 +197,7 @@ export class GraphQLWebSocketClient {

rawRequest<T = any, V extends Variables = Variables, E = any>(
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 }
Expand All @@ -207,12 +208,15 @@ export class GraphQLWebSocketClient {
error: reject,
complete: () => resolve(result),
},
variables
variables,
)
})
}

request<T = any, V extends Variables = Variables>(document: RequestDocument, variables?: V): Promise<T> {
request<T = any, V extends Variables = Variables>(
document: RequestDocument | TypedDocumentNode<T, V>,
variables?: V,
): Promise<T> {
return new Promise<T>((resolve, reject) => {
let result: T
this.subscribe(
Expand All @@ -222,15 +226,15 @@ export class GraphQLWebSocketClient {
error: reject,
complete: () => resolve(result),
},
variables
variables,
)
})
}

subscribe<T = any, V extends Variables = Variables, E = any>(
document: RequestDocument,
document: RequestDocument | TypedDocumentNode<T, V>,
subscriber: GraphQLSubscriber<T, E>,
variables?: V
variables?: V,
): UnsubscribeCallback {
const { query, operationName } = resolveRequestDocument(document)
return this.makeSubscribe(query, operationName, subscriber, variables)
Expand All @@ -239,7 +243,7 @@ export class GraphQLWebSocketClient {
rawSubscribe<T = any, V extends Variables = Variables, E = any>(
query: string,
subscriber: GraphQLSubscriber<T, E>,
variables?: V
variables?: V,
): UnsubscribeCallback {
return this.makeSubscribe(query, undefined, subscriber, variables)
}
Expand Down

0 comments on commit a9fcb44

Please sign in to comment.