@kubun/with-apollo 0.10.0 → 0.12.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.js +82 -1
- package/package.json +18 -13
package/lib/index.js
CHANGED
|
@@ -1 +1,82 @@
|
|
|
1
|
-
import{ApolloLink
|
|
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,34 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/with-apollo",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"license": "see LICENSE.md",
|
|
3
|
+
"version": "0.12.0",
|
|
5
4
|
"keywords": [],
|
|
5
|
+
"license": "see LICENSE.md",
|
|
6
|
+
"sideEffects": false,
|
|
6
7
|
"type": "module",
|
|
7
|
-
"main": "lib/index.js",
|
|
8
|
-
"types": "lib/index.d.ts",
|
|
9
8
|
"exports": {
|
|
10
9
|
".": "./lib/index.js"
|
|
11
10
|
},
|
|
11
|
+
"main": "lib/index.js",
|
|
12
|
+
"types": "lib/index.d.ts",
|
|
12
13
|
"files": [
|
|
13
14
|
"lib/*",
|
|
14
15
|
"LICENSE.md"
|
|
15
16
|
],
|
|
16
|
-
"sideEffects": false,
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@apollo/client": "^4.2.
|
|
19
|
-
"graphql": "^16.
|
|
18
|
+
"@apollo/client": "^4.2.10",
|
|
19
|
+
"graphql": "^16.14.2"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@
|
|
22
|
+
"@enkaku/transport": "^0.20.0",
|
|
23
|
+
"@kubun/engine": "^0.12.0",
|
|
24
|
+
"@kubun/plugin-rpc": "^0.12.0",
|
|
25
|
+
"@kubun/client": "^0.12.0",
|
|
26
|
+
"@kubun/test-utils": "^0.12.0",
|
|
27
|
+
"@kubun/protocol": "^0.12.0"
|
|
23
28
|
},
|
|
24
29
|
"scripts": {
|
|
30
|
+
"build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
|
|
25
31
|
"build:clean": "del lib",
|
|
26
|
-
"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",
|
|
27
33
|
"build:types": "tsc --emitDeclarationOnly --skipLibCheck",
|
|
28
34
|
"build:types:ci": "tsc --emitDeclarationOnly --declarationMap false --skipLibCheck",
|
|
29
|
-
"
|
|
30
|
-
"test:types": "tsc --noEmit --skipLibCheck",
|
|
31
|
-
"test:unit": "vitest run"
|
|
32
|
-
"test": "pnpm run test:types && pnpm run test:unit"
|
|
35
|
+
"test": "pnpm run test:types && pnpm run test:unit",
|
|
36
|
+
"test:types": "tsc --noEmit --skipLibCheck -p tsconfig.test.json",
|
|
37
|
+
"test:unit": "vitest run"
|
|
33
38
|
}
|
|
34
39
|
}
|