UNPKG

1.38 kBPlain TextView Raw
1import { ApolloLink, Operation, FetchResult, Observable } from 'apollo-link';
2
3import { SubscriptionClient, ClientOptions } from 'subscriptions-transport-ws';
4
5export namespace WebSocketLink {
6 /**
7 * Configuration to use when constructing the subscription client (subscriptions-transport-ws).
8 */
9 export interface Configuration {
10 /**
11 * The endpoint to connect to.
12 */
13 uri: string;
14
15 /**
16 * Options to pass when constructing the subscription client.
17 */
18 options?: ClientOptions;
19
20 /**
21 * A custom WebSocket implementation to use.
22 */
23 webSocketImpl?: any;
24 }
25}
26
27// For backwards compatibility.
28export import WebSocketParams = WebSocketLink.Configuration;
29
30export 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}