-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
index.ts
57 lines (48 loc) · 1.48 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { ClientOptions } from "subscriptions-transport-ws";
import { SubscriptionClient } from "subscriptions-transport-ws";
import type { Operation, FetchResult } from "../core/index.js";
import { ApolloLink } from "../core/index.js";
import type { Observable } from "../../utilities/index.js";
export namespace WebSocketLink {
/**
* Configuration to use when constructing the subscription client (subscriptions-transport-ws).
*/
export interface Configuration {
/**
* The endpoint to connect to.
*/
uri: string;
/**
* Options to pass when constructing the subscription client.
*/
options?: ClientOptions;
/**
* A custom WebSocket implementation to use.
*/
webSocketImpl?: any;
}
}
// For backwards compatibility.
export import WebSocketParams = WebSocketLink.Configuration;
export class WebSocketLink extends ApolloLink {
private subscriptionClient: SubscriptionClient;
constructor(
paramsOrClient: WebSocketLink.Configuration | SubscriptionClient
) {
super();
if (paramsOrClient instanceof SubscriptionClient) {
this.subscriptionClient = paramsOrClient;
} else {
this.subscriptionClient = new SubscriptionClient(
paramsOrClient.uri,
paramsOrClient.options,
paramsOrClient.webSocketImpl
);
}
}
public request(operation: Operation): Observable<FetchResult> | null {
return this.subscriptionClient.request(
operation
) as Observable<FetchResult>;
}
}