@kubun/with-apollo 0.17.2 → 0.18.0

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/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ApolloLink } from '@apollo/client';
2
+ import { type SubscriptionRecoveryOptions } from '@kubun/client';
2
3
  import type { GraphQLSource } from '@kubun/protocol';
3
4
  /**
4
5
  * The connector operation-envelope ids a mutation carries (see
@@ -15,4 +16,11 @@ export type OperationEnvelope = {
15
16
  * link, for a mutation (`client.mutate({ context })` / `useMutation`).
16
17
  */
17
18
  export declare function operationEnvelopeContext(envelope: OperationEnvelope): Record<string, unknown>;
18
- export declare function createLink(source: GraphQLSource): ApolloLink;
19
+ export type LinkOptions = SubscriptionRecoveryOptions & {
20
+ /**
21
+ * Makes the client's data consistent again after a subscription recovered: events sent
22
+ * while it was down are lost. Defaults to refetching the client's active queries.
23
+ */
24
+ rebaseline?: (operation: ApolloLink.Operation) => Promise<unknown>;
25
+ };
26
+ export declare function createLink(source: GraphQLSource, options?: LinkOptions): ApolloLink;
package/lib/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { ApolloLink, Observable } from '@apollo/client';
2
2
  import { getMainDefinition, print } from '@apollo/client/utilities';
3
+ import { runRecoveringSubscription } from '@kubun/client';
3
4
  const OPERATION_ENVELOPE_KEY = 'kubunOperationEnvelope';
4
5
  /**
5
6
  * The Apollo operation `context` that carries an operation envelope to the
@@ -46,7 +47,12 @@ function observableWithError(error) {
46
47
  subscriber.error(error);
47
48
  });
48
49
  }
49
- export function createLink(source) {
50
+ function refetchActiveQueries(operation) {
51
+ return Promise.resolve(operation.client.refetchQueries({
52
+ include: 'active'
53
+ }));
54
+ }
55
+ export function createLink(source, options = {}) {
50
56
  return new ApolloLink((operation)=>{
51
57
  const definition = getMainDefinition(operation.query);
52
58
  if (definition.kind !== 'OperationDefinition') {
@@ -57,47 +63,21 @@ export function createLink(source) {
57
63
  variables: operation.variables
58
64
  };
59
65
  if (definition.operation === 'subscription') {
66
+ const rebaseline = options.rebaseline ?? refetchActiveQueries;
67
+ // The returned close also wins a race against the subscribe resolving, and closing
68
+ // the consumer generator propagates through the client and transport to tear down
69
+ // the server-side subscription.
60
70
  return new Observable((observer)=>{
61
- let cancelled = false;
62
- let generator;
63
- source.subscribe(params).then((result)=>{
64
- if (Symbol.asyncIterator in result) {
65
- generator = result;
66
- // Unsubscribe may win the race against this resolution: close the
67
- // server subscription immediately rather than draining it forever.
68
- if (cancelled) {
69
- void generator.return?.(undefined);
70
- return;
71
- }
72
- ;
73
- (async ()=>{
74
- try {
75
- for await (const value of generator){
76
- if (cancelled) break;
77
- observer.next(value);
78
- }
79
- if (!cancelled) {
80
- observer.complete();
81
- }
82
- } catch (err) {
83
- if (!cancelled) {
84
- observer.error(err);
85
- }
86
- }
87
- })();
88
- } else {
89
- observer.next(result);
90
- observer.complete();
91
- }
92
- }, (err)=>{
93
- observer.error(err);
71
+ return runRecoveringSubscription({
72
+ interruptedBackoff: options.interruptedBackoff,
73
+ failedBackoff: options.failedBackoff,
74
+ schedule: options.schedule,
75
+ subscribe: ()=>source.subscribe(params),
76
+ rebaseline: ()=>rebaseline(operation),
77
+ next: (value)=>observer.next(value),
78
+ error: (error)=>observer.error(error),
79
+ complete: ()=>observer.complete()
94
80
  });
95
- // Closing the consumer generator propagates through the client and
96
- // transport to tear down the server-side subscription.
97
- return ()=>{
98
- cancelled = true;
99
- void generator?.return?.(undefined);
100
- };
101
81
  });
102
82
  }
103
83
  // query or mutation -- route to the appropriate source method
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/with-apollo",
3
- "version": "0.17.2",
3
+ "version": "0.18.0",
4
4
  "keywords": [],
5
5
  "license": "see LICENSE.md",
6
6
  "sideEffects": false,
@@ -15,16 +15,16 @@
15
15
  "LICENSE.md"
16
16
  ],
17
17
  "dependencies": {
18
- "@apollo/client": "^4.3.1",
18
+ "@apollo/client": "^4.3.5",
19
+ "@kubun/client": "^0.18.0",
19
20
  "graphql": "^17.0.2"
20
21
  },
21
22
  "devDependencies": {
22
- "@enkaku/transport": "^0.21.0",
23
- "@kubun/client": "^0.17.2",
24
- "@kubun/engine": "^0.17.2",
25
- "@kubun/plugin-service-server": "^0.17.1",
26
- "@kubun/protocol": "^0.17.1",
27
- "@kubun/service-graph-api": "^0.17.2",
23
+ "@enkaku/transport": "^0.21.5",
24
+ "@kubun/engine": "^0.18.0",
25
+ "@kubun/plugin-service-server": "^0.18.0",
26
+ "@kubun/protocol": "^0.18.0",
27
+ "@kubun/service-graph-api": "^0.18.0",
28
28
  "@kubun/test-utils": "^0.0.0"
29
29
  },
30
30
  "publishConfig": {