@rxdi/graphql-client 0.7.251 → 0.7.252
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -0
- package/dist/graphql.injection.d.ts +59 -0
- package/dist/graphql.injection.js +42 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +36 -18
- package/dist/subscription-client.d.ts +23 -0
- package/dist/subscription-client.js +55 -0
- package/package.json +26 -5
package/README.md
CHANGED
|
@@ -184,6 +184,80 @@ export class DetailsComponent extends BaseComponent {
|
|
|
184
184
|
```
|
|
185
185
|
|
|
186
186
|
|
|
187
|
+
# Subscription connection errors and the subscription client
|
|
188
|
+
|
|
189
|
+
The WebSocket link is a lazy `subscriptions-transport-ws` `SubscriptionClient`
|
|
190
|
+
with `reconnect: true`. Two things follow from that, and both are the
|
|
191
|
+
consumer's to handle:
|
|
192
|
+
|
|
193
|
+
- **A rejected handshake errors every pending subscription.** The server's
|
|
194
|
+
`onConnect` rejection reaches each operation's observable as a bare
|
|
195
|
+
`{ message }` (no `graphQLErrors`). That is the deterministic signal that
|
|
196
|
+
the connection is dead.
|
|
197
|
+
- **The library never stops the reconnect loop by itself.** After the server
|
|
198
|
+
closes the socket the client re-handshakes with backoff (capped at 10 s)
|
|
199
|
+
forever, re-sending the same `connectionParams`. For a dead session that
|
|
200
|
+
is a request to your auth backend every few seconds per open tab.
|
|
201
|
+
|
|
202
|
+
So the module exposes the client and a hook:
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
import {
|
|
206
|
+
GraphqlModule,
|
|
207
|
+
GraphqlSubscriptionClient,
|
|
208
|
+
isAuthorizationConnectionError,
|
|
209
|
+
} from '@rxdi/graphql-client';
|
|
210
|
+
|
|
211
|
+
GraphqlModule.forRoot({
|
|
212
|
+
uri, pubsub,
|
|
213
|
+
pubsubOptions: { connectionParams: () => ({ 'x-session-id': session.id }) },
|
|
214
|
+
// Handshake rejections only (never success). Fires BEFORE the operation
|
|
215
|
+
// errors, so do not close the client from here — log or flag state.
|
|
216
|
+
onSubscriptionConnectionError: (error) => console.warn('ws rejected', error),
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// Where you handle the operation error (an effect, a service, ...):
|
|
220
|
+
@Injectable()
|
|
221
|
+
class SessionService {
|
|
222
|
+
@Inject(GraphqlSubscriptionClient) private ws: GraphqlSubscriptionClient;
|
|
223
|
+
|
|
224
|
+
onSubscriptionError(error: unknown) {
|
|
225
|
+
if (isAuthorizationConnectionError(error)) {
|
|
226
|
+
// Session is dead: stop re-sending it. Works in every client state
|
|
227
|
+
// (a plain close(true, true) is a no-op between reconnect attempts)
|
|
228
|
+
// and errors any other still-pending subscription with the reason.
|
|
229
|
+
this.ws.terminate(error instanceof Error ? error : undefined);
|
|
230
|
+
// ...show your "session expired" UI. Once credentials are renewed,
|
|
231
|
+
// simply subscribe again: the lazy link reconnects and re-evaluates
|
|
232
|
+
// connectionParams.
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Why not terminate inside `onSubscriptionConnectionError`: the connection-level
|
|
239
|
+
error arrives before the per-operation errors. Stopping the client there
|
|
240
|
+
settles the operations before the real rejection reaches them, so the app
|
|
241
|
+
never learns the session died; a non-forced close (`close(false, false)`)
|
|
242
|
+
schedules a reconnect. The decision belongs where the operation error is
|
|
243
|
+
observed.
|
|
244
|
+
|
|
245
|
+
`GraphqlSubscriptionClient` resolves to a `ManagedSubscriptionClient`: the
|
|
246
|
+
stock client plus `terminate(reason?)`. Prefer it over `close(true, true)`,
|
|
247
|
+
which does nothing between two reconnect attempts (no socket object exists
|
|
248
|
+
then) and leaves the reconnect timer armed.
|
|
249
|
+
|
|
250
|
+
`refreshOnUnauthenticated: true` keeps its exact old meaning —
|
|
251
|
+
`location.reload()` when the rejection message is the literal `Unauthorized`,
|
|
252
|
+
and nothing else. It is deliberately NOT widened: a consumer whose server says
|
|
253
|
+
`You are not authorized` never reloaded before, and silently turning that into
|
|
254
|
+
a reload could loop a page whose credentials are launch-bound (an operator's
|
|
255
|
+
session id). Use `isAuthorizationConnectionError` in your own hook when you
|
|
256
|
+
want the wider match.
|
|
257
|
+
|
|
258
|
+
The contract above is pinned by `src/index.spec.ts` against a real
|
|
259
|
+
subscriptions-transport-ws server (`npm test`).
|
|
260
|
+
|
|
187
261
|
# Advanced features
|
|
188
262
|
|
|
189
263
|
|
|
@@ -2,9 +2,36 @@ import { InjectionToken } from '@rxdi/core';
|
|
|
2
2
|
import { ApolloClient as AC, ApolloClientOptions, DocumentNode, HttpOptions, RequestHandler, TypedDocumentNode } from '@apollo/client/core';
|
|
3
3
|
import { NormalizedCacheObject, InMemoryCache } from '@apollo/client/cache';
|
|
4
4
|
import { ClientOptions } from 'subscriptions-transport-ws';
|
|
5
|
+
import { ManagedSubscriptionClient } from './subscription-client';
|
|
5
6
|
export declare const ApolloClient: InjectionToken<AC<NormalizedCacheObject>>;
|
|
6
7
|
export interface ApolloClient extends AC<NormalizedCacheObject> {
|
|
7
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* The subscriptions-transport-ws client behind the module's WebSocket link
|
|
11
|
+
* (a ManagedSubscriptionClient — the stock client plus `terminate()`).
|
|
12
|
+
*
|
|
13
|
+
* Exposed so a consumer can act on the connection itself — the one thing the
|
|
14
|
+
* library cannot decide correctly on its behalf. The typical use is a dead
|
|
15
|
+
* session: the server rejects `connection_init`, every pending subscription
|
|
16
|
+
* errors, and `reconnect: true` would otherwise re-send the same dead
|
|
17
|
+
* credentials forever (backoff-capped at 10 s). The consumer, who SEES the
|
|
18
|
+
* operation error, calls `terminate()` to stop that; the link is lazy, so
|
|
19
|
+
* the next `subscribe()` opens a fresh connection with freshly evaluated
|
|
20
|
+
* `connectionParams`.
|
|
21
|
+
*
|
|
22
|
+
* Use `terminate()`, not `close(true, true)`: the latter is a no-op between
|
|
23
|
+
* two reconnect attempts (no socket object exists then) and leaves the
|
|
24
|
+
* reconnect timer armed — see ManagedSubscriptionClient.
|
|
25
|
+
*
|
|
26
|
+
* Why the library does not stop it itself: the connection-level error
|
|
27
|
+
* arrives BEFORE the per-operation errors, and a forced close there drops
|
|
28
|
+
* the operations without erroring them, so the app never learns the
|
|
29
|
+
* session died. A non-forced close reconnects. There is no right call to
|
|
30
|
+
* make from `connectionCallback` — see `onSubscriptionConnectionError`.
|
|
31
|
+
*/
|
|
32
|
+
export declare const GraphqlSubscriptionClient: InjectionToken<ManagedSubscriptionClient>;
|
|
33
|
+
export interface GraphqlSubscriptionClient extends ManagedSubscriptionClient {
|
|
34
|
+
}
|
|
8
35
|
export declare const GraphqlDocuments: InjectionToken<Record<string, DocumentNode | TypedDocumentNode<{
|
|
9
36
|
[key: string]: any;
|
|
10
37
|
}, {
|
|
@@ -18,10 +45,42 @@ export interface GraphqlModuleConfig {
|
|
|
18
45
|
apolloRequestHandler?: RequestHandler;
|
|
19
46
|
cancelPendingRequests?: boolean;
|
|
20
47
|
apolloClientOptions?: ApolloClientOptions<unknown>;
|
|
48
|
+
/**
|
|
49
|
+
* `location.reload()` when the WebSocket handshake is rejected with the
|
|
50
|
+
* literal message `Unauthorized` — the exact legacy behaviour, kept
|
|
51
|
+
* byte-for-byte so no existing consumer starts reloading on a wording it
|
|
52
|
+
* never reloaded on (a reload only helps where reloading obtains new
|
|
53
|
+
* credentials; with launch-bound credentials it loops). Wider matching is
|
|
54
|
+
* available for your own logic through `isAuthorizationConnectionError`
|
|
55
|
+
* in `onSubscriptionConnectionError`.
|
|
56
|
+
*/
|
|
21
57
|
refreshOnUnauthenticated?: boolean;
|
|
22
58
|
httpOptions?: HttpOptions;
|
|
59
|
+
/**
|
|
60
|
+
* Spread LAST over the module's own SubscriptionClient options, so anything
|
|
61
|
+
* here (`connectionParams`, `reconnect`, `connectionCallback`, ...) wins.
|
|
62
|
+
*/
|
|
23
63
|
pubsubOptions?: ClientOptions;
|
|
64
|
+
/**
|
|
65
|
+
* Called with the server's `connection_error` payload whenever the
|
|
66
|
+
* WebSocket handshake is rejected (never on success), plus the client so
|
|
67
|
+
* the consumer can act on it. Fires BEFORE the pending operations receive
|
|
68
|
+
* their own errors, so do not `terminate()` from here — that settles them
|
|
69
|
+
* before the real rejection reaches them (see `GraphqlSubscriptionClient`).
|
|
70
|
+
* Log, flag state, or defer; stop reconnection from the operation error
|
|
71
|
+
* handler instead.
|
|
72
|
+
*/
|
|
73
|
+
onSubscriptionConnectionError?(error: unknown, client: ManagedSubscriptionClient): void;
|
|
24
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* True when a WebSocket `connection_error` payload is an authorization
|
|
77
|
+
* rejection. Servers phrase it differently — subscriptions-transport-ws
|
|
78
|
+
* forwards whatever the `onConnect` hook threw: a bare 'Unauthorized', Boom's
|
|
79
|
+
* 'You are not authorized', ... — and the library matched only the first
|
|
80
|
+
* literal for years, which is how a backend switching to the second silently
|
|
81
|
+
* turned a dead session into an infinite reconnect loop.
|
|
82
|
+
*/
|
|
83
|
+
export declare function isAuthorizationConnectionError(error: unknown): boolean;
|
|
25
84
|
export declare const noopHeaders: () => Headers;
|
|
26
85
|
export declare const noop: () => any;
|
|
27
86
|
export interface Definintion {
|
|
@@ -1,9 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noop = exports.noopHeaders = exports.GraphqlDocuments = exports.ApolloClient = void 0;
|
|
3
|
+
exports.noop = exports.noopHeaders = exports.GraphqlDocuments = exports.GraphqlSubscriptionClient = exports.ApolloClient = void 0;
|
|
4
|
+
exports.isAuthorizationConnectionError = isAuthorizationConnectionError;
|
|
4
5
|
const core_1 = require("@rxdi/core");
|
|
5
6
|
exports.ApolloClient = new core_1.InjectionToken('apollo-link');
|
|
7
|
+
/**
|
|
8
|
+
* The subscriptions-transport-ws client behind the module's WebSocket link
|
|
9
|
+
* (a ManagedSubscriptionClient — the stock client plus `terminate()`).
|
|
10
|
+
*
|
|
11
|
+
* Exposed so a consumer can act on the connection itself — the one thing the
|
|
12
|
+
* library cannot decide correctly on its behalf. The typical use is a dead
|
|
13
|
+
* session: the server rejects `connection_init`, every pending subscription
|
|
14
|
+
* errors, and `reconnect: true` would otherwise re-send the same dead
|
|
15
|
+
* credentials forever (backoff-capped at 10 s). The consumer, who SEES the
|
|
16
|
+
* operation error, calls `terminate()` to stop that; the link is lazy, so
|
|
17
|
+
* the next `subscribe()` opens a fresh connection with freshly evaluated
|
|
18
|
+
* `connectionParams`.
|
|
19
|
+
*
|
|
20
|
+
* Use `terminate()`, not `close(true, true)`: the latter is a no-op between
|
|
21
|
+
* two reconnect attempts (no socket object exists then) and leaves the
|
|
22
|
+
* reconnect timer armed — see ManagedSubscriptionClient.
|
|
23
|
+
*
|
|
24
|
+
* Why the library does not stop it itself: the connection-level error
|
|
25
|
+
* arrives BEFORE the per-operation errors, and a forced close there drops
|
|
26
|
+
* the operations without erroring them, so the app never learns the
|
|
27
|
+
* session died. A non-forced close reconnects. There is no right call to
|
|
28
|
+
* make from `connectionCallback` — see `onSubscriptionConnectionError`.
|
|
29
|
+
*/
|
|
30
|
+
exports.GraphqlSubscriptionClient = new core_1.InjectionToken('graphql-subscription-client');
|
|
6
31
|
exports.GraphqlDocuments = new core_1.InjectionToken('graphql-documents');
|
|
32
|
+
/**
|
|
33
|
+
* True when a WebSocket `connection_error` payload is an authorization
|
|
34
|
+
* rejection. Servers phrase it differently — subscriptions-transport-ws
|
|
35
|
+
* forwards whatever the `onConnect` hook threw: a bare 'Unauthorized', Boom's
|
|
36
|
+
* 'You are not authorized', ... — and the library matched only the first
|
|
37
|
+
* literal for years, which is how a backend switching to the second silently
|
|
38
|
+
* turned a dead session into an infinite reconnect loop.
|
|
39
|
+
*/
|
|
40
|
+
function isAuthorizationConnectionError(error) {
|
|
41
|
+
var _a;
|
|
42
|
+
if (!error) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const message = typeof error === 'string' ? error : String((_a = error.message) !== null && _a !== void 0 ? _a : '');
|
|
46
|
+
return /unauthorized|not authorized/i.test(message);
|
|
47
|
+
}
|
|
7
48
|
const noopHeaders = () => new Headers();
|
|
8
49
|
exports.noopHeaders = noopHeaders;
|
|
9
50
|
const noop = () => null;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { ModuleWithServices } from '@rxdi/core';
|
|
2
2
|
import { GraphqlModuleConfig } from './graphql.injection';
|
|
3
3
|
export declare class GraphqlModule {
|
|
4
|
-
static forRoot({ uri, pubsub, refreshOnUnauthenticated, onRequest, cache, apolloRequestHandler, cancelPendingRequests, apolloClientOptions, httpOptions, pubsubOptions }?: GraphqlModuleConfig, documents?: {}): ModuleWithServices;
|
|
4
|
+
static forRoot({ uri, pubsub, refreshOnUnauthenticated, onRequest, cache, apolloRequestHandler, cancelPendingRequests, apolloClientOptions, httpOptions, pubsubOptions, onSubscriptionConnectionError, }?: GraphqlModuleConfig, documents?: {}): ModuleWithServices;
|
|
5
5
|
}
|
|
6
6
|
export * from './graphql.injection';
|
|
7
|
+
export * from './subscription-client';
|
|
7
8
|
export * from './graphq.helpers';
|
|
8
9
|
export { GraphQLRequest, QueryOptions, SubscriptionOptions, MutationOptions, PossibleTypesMap, } from '@apollo/client/core';
|
|
9
10
|
export { InMemoryCache } from '@apollo/client/cache';
|
package/dist/index.js
CHANGED
|
@@ -36,16 +36,48 @@ const cache_1 = require("@apollo/client/cache");
|
|
|
36
36
|
const graphql_injection_1 = require("./graphql.injection");
|
|
37
37
|
const core_2 = require("@apollo/client/core");
|
|
38
38
|
const ws_1 = require("@apollo/client/link/ws");
|
|
39
|
-
const
|
|
39
|
+
const subscription_client_1 = require("./subscription-client");
|
|
40
40
|
const utilities_1 = require("@apollo/client/utilities");
|
|
41
41
|
const context_1 = require("@apollo/client/link/context");
|
|
42
42
|
let GraphqlModule = GraphqlModule_1 = class GraphqlModule {
|
|
43
|
-
static forRoot({ uri, pubsub, refreshOnUnauthenticated, onRequest, cache, apolloRequestHandler, cancelPendingRequests, apolloClientOptions, httpOptions = {}, pubsubOptions = {} } = {}, documents = {}) {
|
|
43
|
+
static forRoot({ uri, pubsub, refreshOnUnauthenticated, onRequest, cache, apolloRequestHandler, cancelPendingRequests, apolloClientOptions, httpOptions = {}, pubsubOptions = {}, onSubscriptionConnectionError, } = {}, documents = {}) {
|
|
44
44
|
const headers = {};
|
|
45
45
|
const connections = {};
|
|
46
|
+
// One client for the WebSocket link AND the GraphqlSubscriptionClient
|
|
47
|
+
// token, so what a consumer closes is what the link uses. `lazy: true`:
|
|
48
|
+
// nothing connects until the first subscription, and a client the
|
|
49
|
+
// consumer force-closed reconnects on the next one — with freshly
|
|
50
|
+
// evaluated connectionParams.
|
|
51
|
+
const subscriptionClient = new subscription_client_1.ManagedSubscriptionClient(pubsub, Object.assign({ lazy: true, connectionParams: () => ({
|
|
52
|
+
get authorization() {
|
|
53
|
+
return headers['authorization'];
|
|
54
|
+
},
|
|
55
|
+
}), connectionCallback: (error) => {
|
|
56
|
+
// Also fires on every successful ack, with no error.
|
|
57
|
+
if (!error) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
console.error('[Subscription]: ', error);
|
|
61
|
+
// Legacy reload: the literal 'Unauthorized' only, exactly as before
|
|
62
|
+
// (see GraphqlModuleConfig.refreshOnUnauthenticated).
|
|
63
|
+
if (refreshOnUnauthenticated && (error === null || error === void 0 ? void 0 : error['message']) === 'Unauthorized') {
|
|
64
|
+
location.reload();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
// Deliberately NO close()/terminate() here: this fires before the
|
|
68
|
+
// pending operations receive their errors, so stopping the client
|
|
69
|
+
// now settles them with nothing (forced close) or reconnects
|
|
70
|
+
// (non-forced). Stopping the reconnect loop is the consumer's call,
|
|
71
|
+
// from the operation error it does see — see GraphqlSubscriptionClient.
|
|
72
|
+
onSubscriptionConnectionError === null || onSubscriptionConnectionError === void 0 ? void 0 : onSubscriptionConnectionError(error, subscriptionClient);
|
|
73
|
+
}, reconnect: true }, pubsubOptions));
|
|
46
74
|
return {
|
|
47
75
|
module: GraphqlModule_1,
|
|
48
76
|
providers: [
|
|
77
|
+
{
|
|
78
|
+
provide: graphql_injection_1.GraphqlSubscriptionClient,
|
|
79
|
+
useValue: subscriptionClient,
|
|
80
|
+
},
|
|
49
81
|
{
|
|
50
82
|
provide: graphql_injection_1.GraphqlDocuments,
|
|
51
83
|
useValue: Object.keys(documents).reduce((prev, doc) => (Object.assign(Object.assign({}, prev), { [doc.split('/').pop()]: documents[doc] })), {}),
|
|
@@ -104,22 +136,7 @@ let GraphqlModule = GraphqlModule_1 = class GraphqlModule {
|
|
|
104
136
|
]), (0, core_2.split)(({ query }) => {
|
|
105
137
|
const { kind, operation } = (0, utilities_1.getMainDefinition)(query);
|
|
106
138
|
return kind === 'OperationDefinition' && operation === 'subscription';
|
|
107
|
-
}, (()
|
|
108
|
-
const wsLink = new ws_1.WebSocketLink(new subscriptions_transport_ws_1.SubscriptionClient(pubsub, Object.assign({ lazy: true, connectionParams: () => ({
|
|
109
|
-
get authorization() {
|
|
110
|
-
return headers['authorization'];
|
|
111
|
-
},
|
|
112
|
-
}), connectionCallback: (error) => {
|
|
113
|
-
console.error('[Subscription]: ', error);
|
|
114
|
-
if ((error === null || error === void 0 ? void 0 : error['message']) === 'Unauthorized') {
|
|
115
|
-
if (refreshOnUnauthenticated) {
|
|
116
|
-
location.reload();
|
|
117
|
-
}
|
|
118
|
-
wsLink['subscriptionClient'].close(false, false);
|
|
119
|
-
}
|
|
120
|
-
}, reconnect: true }, pubsubOptions)));
|
|
121
|
-
return wsLink;
|
|
122
|
-
})(), (0, core_2.createHttpLink)(Object.assign({ uri }, httpOptions)))), cache: cache || new cache_1.InMemoryCache() }, apolloClientOptions)),
|
|
139
|
+
}, new ws_1.WebSocketLink(subscriptionClient), (0, core_2.createHttpLink)(Object.assign({ uri }, httpOptions)))), cache: cache || new cache_1.InMemoryCache() }, apolloClientOptions)),
|
|
123
140
|
},
|
|
124
141
|
],
|
|
125
142
|
};
|
|
@@ -130,6 +147,7 @@ exports.GraphqlModule = GraphqlModule = GraphqlModule_1 = __decorate([
|
|
|
130
147
|
(0, core_1.Module)({})
|
|
131
148
|
], GraphqlModule);
|
|
132
149
|
__exportStar(require("./graphql.injection"), exports);
|
|
150
|
+
__exportStar(require("./subscription-client"), exports);
|
|
133
151
|
__exportStar(require("./graphq.helpers"), exports);
|
|
134
152
|
var cache_2 = require("@apollo/client/cache");
|
|
135
153
|
Object.defineProperty(exports, "InMemoryCache", { enumerable: true, get: function () { return cache_2.InMemoryCache; } });
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SubscriptionClient } from 'subscriptions-transport-ws';
|
|
2
|
+
/**
|
|
3
|
+
* subscriptions-transport-ws's SubscriptionClient plus the one operation it
|
|
4
|
+
* lacks: a stop that works in EVERY state.
|
|
5
|
+
*
|
|
6
|
+
* `close(true, true)` only acts while a socket object exists. Between two
|
|
7
|
+
* reconnect attempts the socket field is null, so it returns early and the
|
|
8
|
+
* armed reconnect timer fires anyway — a consumer calling it straight from an
|
|
9
|
+
* operation error (socket still there) is fine; one calling it a moment
|
|
10
|
+
* later (from an effect, after a dispatch, on the next tick) is not, and the
|
|
11
|
+
* dead session keeps re-handshaking. terminate() covers both states.
|
|
12
|
+
*
|
|
13
|
+
* It also settles the operations still pending at that moment. A forced
|
|
14
|
+
* close drops them silently — their observers never hear anything again —
|
|
15
|
+
* and the connection-level rejection reaches the client BEFORE the
|
|
16
|
+
* per-operation errors, so a consumer reacting to the FIRST operation's
|
|
17
|
+
* error would otherwise strand every other one. With a `reason` they error
|
|
18
|
+
* with it; without one they complete, which is what "closed by the user"
|
|
19
|
+
* means to an observer.
|
|
20
|
+
*/
|
|
21
|
+
export declare class ManagedSubscriptionClient extends SubscriptionClient {
|
|
22
|
+
terminate(reason?: Error): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManagedSubscriptionClient = void 0;
|
|
4
|
+
const subscriptions_transport_ws_1 = require("subscriptions-transport-ws");
|
|
5
|
+
/**
|
|
6
|
+
* subscriptions-transport-ws's SubscriptionClient plus the one operation it
|
|
7
|
+
* lacks: a stop that works in EVERY state.
|
|
8
|
+
*
|
|
9
|
+
* `close(true, true)` only acts while a socket object exists. Between two
|
|
10
|
+
* reconnect attempts the socket field is null, so it returns early and the
|
|
11
|
+
* armed reconnect timer fires anyway — a consumer calling it straight from an
|
|
12
|
+
* operation error (socket still there) is fine; one calling it a moment
|
|
13
|
+
* later (from an effect, after a dispatch, on the next tick) is not, and the
|
|
14
|
+
* dead session keeps re-handshaking. terminate() covers both states.
|
|
15
|
+
*
|
|
16
|
+
* It also settles the operations still pending at that moment. A forced
|
|
17
|
+
* close drops them silently — their observers never hear anything again —
|
|
18
|
+
* and the connection-level rejection reaches the client BEFORE the
|
|
19
|
+
* per-operation errors, so a consumer reacting to the FIRST operation's
|
|
20
|
+
* error would otherwise strand every other one. With a `reason` they error
|
|
21
|
+
* with it; without one they complete, which is what "closed by the user"
|
|
22
|
+
* means to an observer.
|
|
23
|
+
*/
|
|
24
|
+
class ManagedSubscriptionClient extends subscriptions_transport_ws_1.SubscriptionClient {
|
|
25
|
+
terminate(reason) {
|
|
26
|
+
const internals = this;
|
|
27
|
+
// Detach the pending operations FIRST: close() → unsubscribeAll() would
|
|
28
|
+
// delete them without a word.
|
|
29
|
+
const pending = Object.values(this.operations);
|
|
30
|
+
this.operations = {};
|
|
31
|
+
// Live or connecting socket: the forced close does the whole job.
|
|
32
|
+
this.close(true, true);
|
|
33
|
+
// No socket (between reconnect attempts): close() returned early, so
|
|
34
|
+
// disarm what it would have.
|
|
35
|
+
internals.clearTryReconnectTimeout();
|
|
36
|
+
internals.clearMaxConnectTimeout();
|
|
37
|
+
internals.clearCheckConnectionInterval();
|
|
38
|
+
internals.clearInactivityTimeout();
|
|
39
|
+
internals.reconnecting = false;
|
|
40
|
+
internals.closedByUser = true;
|
|
41
|
+
// GQL_START messages queued for the next connect would revive them.
|
|
42
|
+
internals.unsentMessagesQueue = [];
|
|
43
|
+
// Settle after the teardown, so a handler that subscribes again right
|
|
44
|
+
// away starts a fresh, lazy connect that this close cannot race.
|
|
45
|
+
for (const { handler } of pending) {
|
|
46
|
+
if (reason) {
|
|
47
|
+
handler([reason], null);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
handler(null, null);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.ManagedSubscriptionClient = ManagedSubscriptionClient;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rxdi/graphql-client",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.252",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"author": "Kristiyan Tachev",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,18 +10,39 @@
|
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "tsc",
|
|
13
|
-
"test": "
|
|
13
|
+
"test": "jest"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@apollo/client": "^3.7.3",
|
|
17
17
|
"subscriptions-transport-ws": "^0.9.19"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@rxdi/core": "^0.7.
|
|
20
|
+
"@rxdi/core": "^0.7.251",
|
|
21
|
+
"@types/jest": "^29.5.0",
|
|
22
|
+
"graphql": "^16.12.0",
|
|
23
|
+
"jest": "^29.7.0",
|
|
24
|
+
"ts-jest": "^29.1.0",
|
|
21
25
|
"typescript": "^5.9.3",
|
|
22
|
-
"
|
|
26
|
+
"ws": "6.2.1"
|
|
23
27
|
},
|
|
24
28
|
"types": "./dist/index.d.ts",
|
|
25
29
|
"module": "./dist/index.js",
|
|
26
|
-
"typings": "./dist/index.d.ts"
|
|
30
|
+
"typings": "./dist/index.d.ts",
|
|
31
|
+
"jest": {
|
|
32
|
+
"testEnvironment": "node",
|
|
33
|
+
"rootDir": "./",
|
|
34
|
+
"testRegex": "/src/.*\\.spec\\.(ts|tsx|js)$",
|
|
35
|
+
"testPathIgnorePatterns": [
|
|
36
|
+
"/node_modules/",
|
|
37
|
+
"/dist/"
|
|
38
|
+
],
|
|
39
|
+
"transform": {
|
|
40
|
+
"^.+\\.tsx?$": [
|
|
41
|
+
"ts-jest",
|
|
42
|
+
{
|
|
43
|
+
"diagnostics": false
|
|
44
|
+
}
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
}
|
|
27
48
|
}
|