UNPKG

1.91 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import { print } from "graphql";
3import { ApolloLink } from "../core/index.js";
4import { isNonNullObject, Observable } from "../../utilities/index.js";
5import { ApolloError } from "../../errors/index.js";
6function isLikeCloseEvent(val) {
7 return isNonNullObject(val) && "code" in val && "reason" in val;
8}
9function isLikeErrorEvent(err) {
10 var _a;
11 return isNonNullObject(err) && ((_a = err.target) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.CLOSED;
12}
13var GraphQLWsLink = (function (_super) {
14 __extends(GraphQLWsLink, _super);
15 function GraphQLWsLink(client) {
16 var _this = _super.call(this) || this;
17 _this.client = client;
18 return _this;
19 }
20 GraphQLWsLink.prototype.request = function (operation) {
21 var _this = this;
22 return new Observable(function (observer) {
23 return _this.client.subscribe(__assign(__assign({}, operation), { query: print(operation.query) }), {
24 next: observer.next.bind(observer),
25 complete: observer.complete.bind(observer),
26 error: function (err) {
27 if (err instanceof Error) {
28 return observer.error(err);
29 }
30 var likeClose = isLikeCloseEvent(err);
31 if (likeClose || isLikeErrorEvent(err)) {
32 return observer.error(new Error("Socket closed".concat(likeClose ? " with event ".concat(err.code) : "").concat(likeClose ? " ".concat(err.reason) : "")));
33 }
34 return observer.error(new ApolloError({
35 graphQLErrors: Array.isArray(err) ? err : [err],
36 }));
37 },
38 });
39 });
40 };
41 return GraphQLWsLink;
42}(ApolloLink));
43export { GraphQLWsLink };
44//# sourceMappingURL=index.js.map
\No newline at end of file