1 | import { ApolloLink, Operation, FetchResult, Observable } from 'apollo-link';
|
2 |
|
3 | import { SubscriptionClient, ClientOptions } from 'subscriptions-transport-ws';
|
4 |
|
5 | export namespace WebSocketLink {
|
6 | |
7 |
|
8 |
|
9 | export interface Configuration {
|
10 | |
11 |
|
12 |
|
13 | uri: string;
|
14 |
|
15 | |
16 |
|
17 |
|
18 | options?: ClientOptions;
|
19 |
|
20 | |
21 |
|
22 |
|
23 | webSocketImpl?: any;
|
24 | }
|
25 | }
|
26 |
|
27 |
|
28 | export import WebSocketParams = WebSocketLink.Configuration;
|
29 |
|
30 | export class WebSocketLink extends ApolloLink {
|
31 | private subscriptionClient: SubscriptionClient;
|
32 |
|
33 | constructor(
|
34 | paramsOrClient: WebSocketLink.Configuration | SubscriptionClient,
|
35 | ) {
|
36 | super();
|
37 |
|
38 | if (paramsOrClient instanceof SubscriptionClient) {
|
39 | this.subscriptionClient = paramsOrClient;
|
40 | } else {
|
41 | this.subscriptionClient = new SubscriptionClient(
|
42 | paramsOrClient.uri,
|
43 | paramsOrClient.options,
|
44 | paramsOrClient.webSocketImpl,
|
45 | );
|
46 | }
|
47 | }
|
48 |
|
49 | public request(operation: Operation): Observable<FetchResult> | null {
|
50 | return this.subscriptionClient.request(operation) as Observable<
|
51 | FetchResult
|
52 | >;
|
53 | }
|
54 | }
|