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

Introduce GraphQLWebSocketClient #330

Merged
merged 9 commits into from
Apr 23, 2022
Prev Previous commit
Next Next commit
remove subscription client interface
  • Loading branch information
lyubo committed Feb 21, 2022
commit b5e415cdf3ceda58f4e4073466715ecabe2708a1
12 changes: 10 additions & 2 deletions src/graphql-ws.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientError, RequestDocument, Variables, GraphQLSubscriber, GraphQLSubscriptionClient, UnsubscribeCallback } from './types';
import { ClientError, RequestDocument, Variables } from './types';
import * as Dom from './types.dom'
import { resolveRequestDocument } from '.';

Expand Down Expand Up @@ -57,6 +57,14 @@ type SocketHandler = {
onClose?: () => any
}

export type UnsubscribeCallback = () => void;

export interface GraphQLSubscriber<T, E=unknown> {
next?(data: T, extensions?: E): void;
error?(errorValue: ClientError): void;
complete?(): void;
}

type SubscriptionRecord = {
subscriber: GraphQLSubscriber<unknown, unknown>
query: string,
Expand All @@ -69,7 +77,7 @@ type SocketState = {
subscriptions: { [key: string]: SubscriptionRecord }
}
lyubo marked this conversation as resolved.
Show resolved Hide resolved

export class GraphQLWebSocketClient implements GraphQLSubscriptionClient {
export class GraphQLWebSocketClient {

static PROTOCOL: string = "graphql-transport-ws"

Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
BatchRequestDocument,
BatchRequestsOptions,
ClientError,
GraphQLRequestClient,
RawRequestOptions,
RequestDocument,
RequestOptions,
Expand Down Expand Up @@ -187,7 +186,7 @@ const get = async <V = Variables>({
/**
* GraphQL Client.
*/
export class GraphQLClient implements GraphQLRequestClient {
export class GraphQLClient {
private url: string
private options: Dom.RequestInit

Expand Down
39 changes: 0 additions & 39 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,42 +87,3 @@ export type RequestExtendedOptions<V = Variables> = { url: string } & RequestOpt
export type RawRequestExtendedOptions<V = Variables> = { url: string } & RawRequestOptions<V>

export type BatchRequestsExtendedOptions<V = Variables> = { url: string } & BatchRequestsOptions<V>

export interface GraphQLRequestClient {
rawRequest<T = any, V = Variables, E = any>(
query: string,
variables?: V,
requestHeaders?: Dom.RequestInit['headers']
): Promise<{ data: T; extensions?: E; headers?: Dom.Headers; status?: number }>

request<T = any, V = Variables>(
document: RequestDocument,
variables?: V,
requestHeaders?: Dom.RequestInit['headers']
): Promise<T>

}

export type UnsubscribeCallback = () => void;

export interface GraphQLSubscriber<T, E=unknown> {
next?(data: T, extensions?: E): void;
error?(errorValue: ClientError): void;
complete?(): void;
}

export interface GraphQLSubscriptionClient extends GraphQLRequestClient {
subscribe<T = any, V = Variables, E = any>(
document: RequestDocument,
subscriber: GraphQLSubscriber<T, E>,
variables?: V
): UnsubscribeCallback;

rawSubscribe<T = any, V = Variables, E = any>(
query: string,
subscriber: GraphQLSubscriber<T, E>,
variables?: V
): UnsubscribeCallback;

ping(payload: Variables): void;
}