@kubun/with-relay 0.9.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 +78 -1
  2. package/package.json +12 -8
package/lib/index.js CHANGED
@@ -1 +1,78 @@
1
- import{Environment as r,Network as e,Observable as t,RecordSource as n,Store as o}from"relay-runtime";export function createNetwork(r){let{source:n}=r;return e.create((r,e)=>{let o=r.text;if(null==o)return t.create(r=>{r.error(Error("Relay request is missing query text"))});let a={text:o,variables:e},i="mutation"===r.operationKind?n.mutate(a):n.query(a);return t.from(i)},(r,e)=>{let o=r.text;return null==o?t.create(r=>{r.error(Error("Relay request is missing query text"))}):t.create(r=>{n.subscribe({text:o,variables:e}).then(e=>{if(Symbol.asyncIterator in e){let t=!1;return(async()=>{try{for await(let n of e){if(t)break;r.next(n)}t||r.complete()}catch(e){t||r.error(e instanceof Error?e:Error(String(e)))}})(),()=>{t=!0}}r.next(e),r.complete()},e=>{r.error(e instanceof Error?e:Error(String(e)))})})})}export function createEnvironment(e){let{store:t,...a}=e;return new r({network:createNetwork(a),store:t??new o(new n)})}
1
+ import { Environment, Network, Observable, RecordSource, Store } from 'relay-runtime';
2
+ export function createNetwork(params) {
3
+ const { source } = params;
4
+ return Network.create((request, variables)=>{
5
+ const text = request.text;
6
+ if (text == null) {
7
+ return Observable.create((sink)=>{
8
+ sink.error(new Error('Relay request is missing query text'));
9
+ });
10
+ }
11
+ const params = {
12
+ text,
13
+ variables: variables
14
+ };
15
+ const promise = request.operationKind === 'mutation' ? source.mutate(params) : source.query(params);
16
+ return Observable.from(promise);
17
+ }, (request, variables)=>{
18
+ const text = request.text;
19
+ if (text == null) {
20
+ return Observable.create((sink)=>{
21
+ sink.error(new Error('Relay request is missing query text'));
22
+ });
23
+ }
24
+ return Observable.create((sink)=>{
25
+ let disposed = false;
26
+ let generator;
27
+ source.subscribe({
28
+ text,
29
+ variables: variables
30
+ }).then((result)=>{
31
+ if (Symbol.asyncIterator in result) {
32
+ generator = result;
33
+ // Dispose may win the race against this resolution: close the
34
+ // server subscription immediately rather than draining it forever.
35
+ if (disposed) {
36
+ void generator.return?.(undefined);
37
+ return;
38
+ }
39
+ ;
40
+ (async ()=>{
41
+ try {
42
+ for await (const value of generator){
43
+ if (disposed) break;
44
+ sink.next(value);
45
+ }
46
+ if (!disposed) {
47
+ sink.complete();
48
+ }
49
+ } catch (error) {
50
+ if (!disposed) {
51
+ sink.error(error instanceof Error ? error : new Error(String(error)));
52
+ }
53
+ }
54
+ })();
55
+ return;
56
+ }
57
+ sink.next(result);
58
+ sink.complete();
59
+ }, (error)=>{
60
+ sink.error(error instanceof Error ? error : new Error(String(error)));
61
+ });
62
+ // Closing the consumer generator propagates through the client and
63
+ // transport to tear down the server-side subscription. The `disposed`
64
+ // flag still guards sink.next/complete/error after teardown.
65
+ return ()=>{
66
+ disposed = true;
67
+ void generator?.return?.(undefined);
68
+ };
69
+ });
70
+ });
71
+ }
72
+ export function createEnvironment(params) {
73
+ const { store, ...networkParams } = params;
74
+ return new Environment({
75
+ network: createNetwork(networkParams),
76
+ store: store ?? new Store(new RecordSource())
77
+ });
78
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/with-relay",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "license": "see LICENSE.md",
5
5
  "keywords": [],
6
6
  "type": "module",
@@ -15,21 +15,25 @@
15
15
  ],
16
16
  "sideEffects": false,
17
17
  "dependencies": {
18
- "relay-runtime": "^20.1.1"
18
+ "relay-runtime": "^21.0.1"
19
19
  },
20
20
  "devDependencies": {
21
- "@types/relay-runtime": "^20.1.1",
22
- "@kubun/protocol": "^0.9.0",
23
- "@kubun/engine": "^0.9.0",
24
- "@kubun/test-utils": "^0.9.0"
21
+ "@enkaku/transport": "^0.18.1",
22
+ "graphql": "^16.14.2",
23
+ "@kubun/engine": "^0.11.0",
24
+ "@kubun/client": "^0.11.0",
25
+ "@kubun/models": "^0.11.0",
26
+ "@kubun/protocol": "^0.11.0",
27
+ "@kubun/test-utils": "^0.11.0",
28
+ "@kubun/plugin-rpc": "^0.11.0"
25
29
  },
26
30
  "scripts": {
27
31
  "build:clean": "del lib",
28
- "build:js": "swc src -d ./lib --config-file ../../swc.json --strip-leading-paths",
32
+ "build:js": "swc src -d ./lib --config-file ../../node_modules/@kigu/dev/swc.json --strip-leading-paths",
29
33
  "build:types": "tsc --emitDeclarationOnly --skipLibCheck",
30
34
  "build:types:ci": "tsc --emitDeclarationOnly --declarationMap false",
31
35
  "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
32
- "test:types": "tsc --noEmit -p tsconfig.test.json",
36
+ "test:types": "tsc --noEmit --skipLibCheck -p tsconfig.test.json",
33
37
  "test:unit": "vitest run",
34
38
  "test": "pnpm run test:types && pnpm run test:unit"
35
39
  }