@kubun/with-apollo 0.10.0 → 0.11.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.
Files changed (2) hide show
  1. package/lib/index.js +82 -1
  2. package/package.json +11 -6
package/lib/index.js CHANGED
@@ -1 +1,82 @@
1
- import{ApolloLink as e,Observable as r}from"@apollo/client";import{getMainDefinition as t,print as n}from"@apollo/client/utilities";function i(e){return new r(r=>{e.then(e=>{try{r.next(e),r.complete()}catch(e){r.error(e)}},e=>{r.error(e)})})}export function createLink(o){return new e(e=>{let a=t(e.query);if("OperationDefinition"!==a.kind){var c;return c=Error(`Invalid definition: ${a.kind}, expected OperationDefinition`),new r(e=>{e.error(c)})}let l={text:n(e.query),variables:e.variables};return"subscription"===a.operation?new r(e=>{o.subscribe(l).then(r=>{Symbol.asyncIterator in r?(async()=>{try{for await(let t of r)e.next(t);e.complete()}catch(r){e.error(r)}})():(e.next(r),e.complete())},r=>{e.error(r)})}):"mutation"===a.operation?i(o.mutate(l)):i(o.query(l))})}
1
+ import { ApolloLink, Observable } from '@apollo/client';
2
+ import { getMainDefinition, print } from '@apollo/client/utilities';
3
+ function toObservable(promise) {
4
+ return new Observable((observer)=>{
5
+ promise.then((result)=>{
6
+ try {
7
+ observer.next(result);
8
+ observer.complete();
9
+ } catch (err) {
10
+ observer.error(err);
11
+ }
12
+ }, (err)=>{
13
+ observer.error(err);
14
+ });
15
+ });
16
+ }
17
+ function observableWithError(error) {
18
+ return new Observable((subscriber)=>{
19
+ subscriber.error(error);
20
+ });
21
+ }
22
+ export function createLink(source) {
23
+ return new ApolloLink((operation)=>{
24
+ const definition = getMainDefinition(operation.query);
25
+ if (definition.kind !== 'OperationDefinition') {
26
+ return observableWithError(new Error(`Invalid definition: ${definition.kind}, expected OperationDefinition`));
27
+ }
28
+ const params = {
29
+ text: print(operation.query),
30
+ variables: operation.variables
31
+ };
32
+ if (definition.operation === 'subscription') {
33
+ 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);
67
+ });
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
+ });
75
+ }
76
+ // query or mutation -- route to the appropriate source method
77
+ if (definition.operation === 'mutation') {
78
+ return toObservable(source.mutate(params));
79
+ }
80
+ return toObservable(source.query(params));
81
+ });
82
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/with-apollo",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "license": "see LICENSE.md",
5
5
  "keywords": [],
6
6
  "type": "module",
@@ -15,19 +15,24 @@
15
15
  ],
16
16
  "sideEffects": false,
17
17
  "dependencies": {
18
- "@apollo/client": "^4.2.0",
19
- "graphql": "^16.13.2"
18
+ "@apollo/client": "^4.2.5",
19
+ "graphql": "^16.14.2"
20
20
  },
21
21
  "devDependencies": {
22
- "@kubun/protocol": "^0.10.0"
22
+ "@enkaku/transport": "^0.18.1",
23
+ "@kubun/client": "^0.11.0",
24
+ "@kubun/engine": "^0.11.0",
25
+ "@kubun/protocol": "^0.11.0",
26
+ "@kubun/plugin-rpc": "^0.11.0",
27
+ "@kubun/test-utils": "^0.11.0"
23
28
  },
24
29
  "scripts": {
25
30
  "build:clean": "del lib",
26
- "build:js": "swc src -d ./lib --config-file ../../swc.json --strip-leading-paths",
31
+ "build:js": "swc src -d ./lib --config-file ../../node_modules/@kigu/dev/swc.json --strip-leading-paths",
27
32
  "build:types": "tsc --emitDeclarationOnly --skipLibCheck",
28
33
  "build:types:ci": "tsc --emitDeclarationOnly --declarationMap false --skipLibCheck",
29
34
  "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
30
- "test:types": "tsc --noEmit --skipLibCheck",
35
+ "test:types": "tsc --noEmit --skipLibCheck -p tsconfig.test.json",
31
36
  "test:unit": "vitest run",
32
37
  "test": "pnpm run test:types && pnpm run test:unit"
33
38
  }