@kubun/with-apollo 0.17.1 → 0.17.2

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,18 @@
1
1
  import { ApolloLink } from '@apollo/client';
2
2
  import type { GraphQLSource } from '@kubun/protocol';
3
+ /**
4
+ * The connector operation-envelope ids a mutation carries (see
5
+ * `GraphQLOperationParams.operationID`): required on a connector action
6
+ * mutation, the same pair on every retry of one user intent, a new pair for a
7
+ * new intent.
8
+ */
9
+ export type OperationEnvelope = {
10
+ operationID: string;
11
+ deterministicEventID: string;
12
+ };
13
+ /**
14
+ * The Apollo operation `context` that carries an operation envelope to the
15
+ * link, for a mutation (`client.mutate({ context })` / `useMutation`).
16
+ */
17
+ export declare function operationEnvelopeContext(envelope: OperationEnvelope): Record<string, unknown>;
3
18
  export declare function createLink(source: GraphQLSource): ApolloLink;
package/lib/index.js CHANGED
@@ -1,5 +1,32 @@
1
1
  import { ApolloLink, Observable } from '@apollo/client';
2
2
  import { getMainDefinition, print } from '@apollo/client/utilities';
3
+ const OPERATION_ENVELOPE_KEY = 'kubunOperationEnvelope';
4
+ /**
5
+ * The Apollo operation `context` that carries an operation envelope to the
6
+ * link, for a mutation (`client.mutate({ context })` / `useMutation`).
7
+ */ export function operationEnvelopeContext(envelope) {
8
+ return {
9
+ [OPERATION_ENVELOPE_KEY]: {
10
+ operationID: envelope.operationID,
11
+ deterministicEventID: envelope.deterministicEventID
12
+ }
13
+ };
14
+ }
15
+ // The operation envelope an operation's context carries, each id only when it
16
+ // is a string.
17
+ function operationEnvelopeFrom(context) {
18
+ const envelope = context[OPERATION_ENVELOPE_KEY];
19
+ const operationID = envelope?.operationID;
20
+ const deterministicEventID = envelope?.deterministicEventID;
21
+ return {
22
+ ...typeof operationID === 'string' ? {
23
+ operationID
24
+ } : {},
25
+ ...typeof deterministicEventID === 'string' ? {
26
+ deterministicEventID
27
+ } : {}
28
+ };
29
+ }
3
30
  function toObservable(promise) {
4
31
  return new Observable((observer)=>{
5
32
  promise.then((result)=>{
@@ -75,7 +102,10 @@ export function createLink(source) {
75
102
  }
76
103
  // query or mutation -- route to the appropriate source method
77
104
  if (definition.operation === 'mutation') {
78
- return toObservable(source.mutate(params));
105
+ return toObservable(source.mutate({
106
+ ...params,
107
+ ...operationEnvelopeFrom(operation.getContext())
108
+ }));
79
109
  }
80
110
  return toObservable(source.query(params));
81
111
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/with-apollo",
3
- "version": "0.17.1",
3
+ "version": "0.17.2",
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.1",
19
19
  "graphql": "^17.0.2"
20
20
  },
21
21
  "devDependencies": {
22
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
+ "@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",
28
28
  "@kubun/test-utils": "^0.0.0"
29
29
  },
30
30
  "publishConfig": {