1
- // This file is adapted from sample code in the README of the graphql-ws npm package:
1
+ // This file is adapted from the graphql-ws npm package:
2
2
// https://github.com/enisdenjo/graphql-ws
3
3
//
4
+ // Most of the file comes from that package's README; some other parts (such as
5
+ // isLikeCloseEvent) come from its source.
6
+ //
4
7
// Here's the license of the original code:
5
8
//
6
9
// The MIT License (MIT)
@@ -32,6 +35,22 @@ import { ApolloLink, Operation, FetchResult } from "../core";
32
35
import { Observable } from "../../utilities" ;
33
36
import { ApolloError } from "../../errors" ;
34
37
38
+
39
+ function isObject ( val : unknown ) : val is Record < PropertyKey , unknown > {
40
+ return typeof val === 'object' && val !== null ;
41
+ }
42
+ interface LikeCloseEvent {
43
+ /** Returns the WebSocket connection close code provided by the server. */
44
+ readonly code : number ;
45
+ /** Returns the WebSocket connection close reason provided by the server. */
46
+ readonly reason : string ;
47
+ }
48
+
49
+ function isLikeCloseEvent ( val : unknown ) : val is LikeCloseEvent {
50
+ return isObject ( val ) && 'code' in val && 'reason' in val ;
51
+ }
52
+
53
+
35
54
export class GraphQLWsLink extends ApolloLink {
36
55
constructor ( public readonly client : Client ) {
37
56
super ( ) ;
@@ -49,7 +68,7 @@ export class GraphQLWsLink extends ApolloLink {
49
68
return observer . error ( err ) ;
50
69
}
51
70
52
- if ( err instanceof CloseEvent ) {
71
+ if ( isLikeCloseEvent ( err ) ) {
53
72
return observer . error (
54
73
// reason will be available on clean closes
55
74
new Error (
0 commit comments