@kubun/with-apollo 0.17.1 → 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,3 +1,26 @@
1
1
  import { ApolloLink } from '@apollo/client';
2
+ import { type SubscriptionRecoveryOptions } from '@kubun/client';
2
3
  import type { GraphQLSource } from '@kubun/protocol';
3
- export declare function createLink(source: GraphQLSource): ApolloLink;
4
+ /**
5
+ * The connector operation-envelope ids a mutation carries (see
6
+ * `GraphQLOperationParams.operationID`): required on a connector action
7
+ * mutation, the same pair on every retry of one user intent, a new pair for a
8
+ * new intent.
9
+ */
10
+ export type OperationEnvelope = {
11
+ operationID: string;
12
+ deterministicEventID: string;
13
+ };
14
+ /**
15
+ * The Apollo operation `context` that carries an operation envelope to the
16
+ * link, for a mutation (`client.mutate({ context })` / `useMutation`).
17
+ */
18
+ export declare function operationEnvelopeContext(envelope: OperationEnvelope): Record<string, unknown>;
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,33 @@
1
1
  import { ApolloLink, Observable } from '@apollo/client';
2
2
  import { getMainDefinition, print } from '@apollo/client/utilities';
3
+ import { runRecoveringSubscription } from '@kubun/client';
4
+ const OPERATION_ENVELOPE_KEY = 'kubunOperationEnvelope';
5
+ /**
6
+ * The Apollo operation `context` that carries an operation envelope to the
7
+ * link, for a mutation (`client.mutate({ context })` / `useMutation`).
8
+ */ export function operationEnvelopeContext(envelope) {
9
+ return {
10
+ [OPERATION_ENVELOPE_KEY]: {
11
+ operationID: envelope.operationID,
12
+ deterministicEventID: envelope.deterministicEventID
13
+ }
14
+ };
15
+ }
16
+ // The operation envelope an operation's context carries, each id only when it
17
+ // is a string.
18
+ function operationEnvelopeFrom(context) {
19
+ const envelope = context[OPERATION_ENVELOPE_KEY];
20
+ const operationID = envelope?.operationID;
21
+ const deterministicEventID = envelope?.deterministicEventID;
22
+ return {
23
+ ...typeof operationID === 'string' ? {
24
+ operationID
25
+ } : {},
26
+ ...typeof deterministicEventID === 'string' ? {
27
+ deterministicEventID
28
+ } : {}
29
+ };
30
+ }
3
31
  function toObservable(promise) {
4
32
  return new Observable((observer)=>{
5
33
  promise.then((result)=>{
@@ -19,7 +47,12 @@ function observableWithError(error) {
19
47
  subscriber.error(error);
20
48
  });
21
49
  }
22
- 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 = {}) {
23
56
  return new ApolloLink((operation)=>{
24
57
  const definition = getMainDefinition(operation.query);
25
58
  if (definition.kind !== 'OperationDefinition') {
@@ -30,52 +63,29 @@ export function createLink(source) {
30
63
  variables: operation.variables
31
64
  };
32
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.
33
70
  return new Observable((observer)=>{
34
- let cancelled = false;
35
- let generator;
36
- source.subscribe(params).then((result)=>{
37
- if (Symbol.asyncIterator in result) {
38
- generator = result;
39
- // Unsubscribe may win the race against this resolution: close the
40
- // server subscription immediately rather than draining it forever.
41
- if (cancelled) {
42
- void generator.return?.(undefined);
43
- return;
44
- }
45
- ;
46
- (async ()=>{
47
- try {
48
- for await (const value of generator){
49
- if (cancelled) break;
50
- observer.next(value);
51
- }
52
- if (!cancelled) {
53
- observer.complete();
54
- }
55
- } catch (err) {
56
- if (!cancelled) {
57
- observer.error(err);
58
- }
59
- }
60
- })();
61
- } else {
62
- observer.next(result);
63
- observer.complete();
64
- }
65
- }, (err)=>{
66
- 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()
67
80
  });
68
- // Closing the consumer generator propagates through the client and
69
- // transport to tear down the server-side subscription.
70
- return ()=>{
71
- cancelled = true;
72
- void generator?.return?.(undefined);
73
- };
74
81
  });
75
82
  }
76
83
  // query or mutation -- route to the appropriate source method
77
84
  if (definition.operation === 'mutation') {
78
- return toObservable(source.mutate(params));
85
+ return toObservable(source.mutate({
86
+ ...params,
87
+ ...operationEnvelopeFrom(operation.getContext())
88
+ }));
79
89
  }
80
90
  return toObservable(source.query(params));
81
91
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/with-apollo",
3
- "version": "0.17.1",
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.0",
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.1",
24
- "@kubun/engine": "^0.17.1",
25
- "@kubun/plugin-service-server": "^0.17.0",
26
- "@kubun/protocol": "^0.17.0",
27
- "@kubun/service-graph-api": "^0.17.1",
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": {