Skip to content

Commit 464c9c6

Browse files
committed
Adopt isLikeCloseEvent from graphql-ws
1 parent 2c03892 commit 464c9c6

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/link/subscriptions/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
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:
22
// https://github.com/enisdenjo/graphql-ws
33
//
4+
// Most of the file comes from that package's README; some other parts (such as
5+
// isLikeCloseEvent) come from its source.
6+
//
47
// Here's the license of the original code:
58
//
69
// The MIT License (MIT)
@@ -32,6 +35,22 @@ import { ApolloLink, Operation, FetchResult } from "../core";
3235
import { Observable } from "../../utilities";
3336
import { ApolloError } from "../../errors";
3437

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+
3554
export class GraphQLWsLink extends ApolloLink {
3655
constructor(public readonly client: Client) {
3756
super();
@@ -49,7 +68,7 @@ export class GraphQLWsLink extends ApolloLink {
4968
return observer.error(err);
5069
}
5170

52-
if (err instanceof CloseEvent) {
71+
if (isLikeCloseEvent(err)) {
5372
return observer.error(
5473
// reason will be available on clean closes
5574
new Error(

0 commit comments

Comments
 (0)