@graphql-mesh/urql-exchange 0.0.1-alpha-1ea083c35.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/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { Exchange } from '@urql/core';
2
+ import { MeshInstance } from '@graphql-mesh/runtime';
3
+ /** Exchange for executing queries locally on a schema using graphql-js. */
4
+ export declare const meshExchange: (getBuiltMesh: () => Promise<MeshInstance>) => Exchange;
package/index.js ADDED
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const wonka = require('wonka');
6
+ const core = require('@urql/core');
7
+
8
+ const asyncIterator = typeof Symbol !== 'undefined' ? Symbol.asyncIterator : null;
9
+ const makeExecuteSource = (operation, mesh$, document, variables, context, operationName, rootValue) => {
10
+ return wonka.make(observer => {
11
+ let ended = false;
12
+ Promise.resolve(mesh$)
13
+ .then((mesh) => {
14
+ if (ended)
15
+ return;
16
+ if (operation.kind === 'subscription') {
17
+ return mesh.subscribe(document, variables, context, rootValue, operationName);
18
+ }
19
+ return mesh.execute(document, variables, context, rootValue, operationName);
20
+ })
21
+ .then((result) => {
22
+ if (ended || !result) {
23
+ return;
24
+ }
25
+ else if (!asyncIterator || !result[asyncIterator]) {
26
+ observer.next(core.makeResult(operation, result));
27
+ return;
28
+ }
29
+ const iterator = result[asyncIterator]();
30
+ let prevResult = null;
31
+ function next({ done, value }) {
32
+ if (value) {
33
+ observer.next((prevResult = prevResult ? core.mergeResultPatch(prevResult, value) : core.makeResult(operation, value)));
34
+ }
35
+ if (!done && !ended) {
36
+ return iterator.next().then(next);
37
+ }
38
+ if (ended) {
39
+ iterator.return && iterator.return();
40
+ }
41
+ }
42
+ return iterator.next().then(next);
43
+ })
44
+ .then(() => {
45
+ observer.complete();
46
+ })
47
+ .catch(error => {
48
+ observer.next(core.makeErrorResult(operation, error));
49
+ observer.complete();
50
+ });
51
+ return () => {
52
+ ended = true;
53
+ };
54
+ });
55
+ };
56
+ /** Exchange for executing queries locally on a schema using graphql-js. */
57
+ const meshExchange = (getBuiltMesh) => ({ forward }) => {
58
+ const mesh$ = getBuiltMesh();
59
+ return ops$ => {
60
+ const sharedOps$ = wonka.share(ops$);
61
+ const executedOps$ = wonka.pipe(sharedOps$, wonka.filter((operation) => {
62
+ return operation.kind === 'query' || operation.kind === 'mutation' || operation.kind === 'subscription';
63
+ }), wonka.mergeMap((operation) => {
64
+ const { key } = operation;
65
+ const teardown$ = wonka.pipe(sharedOps$, wonka.filter(op => op.kind === 'teardown' && op.key === key));
66
+ return wonka.pipe(makeExecuteSource(operation, mesh$, operation.query, operation.variables, {}, core.getOperationName(operation.query), {}), wonka.takeUntil(teardown$));
67
+ }));
68
+ const forwardedOps$ = wonka.pipe(sharedOps$, wonka.filter(operation => operation.kind === 'teardown'), forward);
69
+ return wonka.merge([executedOps$, forwardedOps$]);
70
+ };
71
+ };
72
+
73
+ exports.meshExchange = meshExchange;
package/index.mjs ADDED
@@ -0,0 +1,69 @@
1
+ import { share, pipe, filter, mergeMap, make, takeUntil, merge } from 'wonka';
2
+ import { getOperationName, makeResult, makeErrorResult, mergeResultPatch } from '@urql/core';
3
+
4
+ const asyncIterator = typeof Symbol !== 'undefined' ? Symbol.asyncIterator : null;
5
+ const makeExecuteSource = (operation, mesh$, document, variables, context, operationName, rootValue) => {
6
+ return make(observer => {
7
+ let ended = false;
8
+ Promise.resolve(mesh$)
9
+ .then((mesh) => {
10
+ if (ended)
11
+ return;
12
+ if (operation.kind === 'subscription') {
13
+ return mesh.subscribe(document, variables, context, rootValue, operationName);
14
+ }
15
+ return mesh.execute(document, variables, context, rootValue, operationName);
16
+ })
17
+ .then((result) => {
18
+ if (ended || !result) {
19
+ return;
20
+ }
21
+ else if (!asyncIterator || !result[asyncIterator]) {
22
+ observer.next(makeResult(operation, result));
23
+ return;
24
+ }
25
+ const iterator = result[asyncIterator]();
26
+ let prevResult = null;
27
+ function next({ done, value }) {
28
+ if (value) {
29
+ observer.next((prevResult = prevResult ? mergeResultPatch(prevResult, value) : makeResult(operation, value)));
30
+ }
31
+ if (!done && !ended) {
32
+ return iterator.next().then(next);
33
+ }
34
+ if (ended) {
35
+ iterator.return && iterator.return();
36
+ }
37
+ }
38
+ return iterator.next().then(next);
39
+ })
40
+ .then(() => {
41
+ observer.complete();
42
+ })
43
+ .catch(error => {
44
+ observer.next(makeErrorResult(operation, error));
45
+ observer.complete();
46
+ });
47
+ return () => {
48
+ ended = true;
49
+ };
50
+ });
51
+ };
52
+ /** Exchange for executing queries locally on a schema using graphql-js. */
53
+ const meshExchange = (getBuiltMesh) => ({ forward }) => {
54
+ const mesh$ = getBuiltMesh();
55
+ return ops$ => {
56
+ const sharedOps$ = share(ops$);
57
+ const executedOps$ = pipe(sharedOps$, filter((operation) => {
58
+ return operation.kind === 'query' || operation.kind === 'mutation' || operation.kind === 'subscription';
59
+ }), mergeMap((operation) => {
60
+ const { key } = operation;
61
+ const teardown$ = pipe(sharedOps$, filter(op => op.kind === 'teardown' && op.key === key));
62
+ return pipe(makeExecuteSource(operation, mesh$, operation.query, operation.variables, {}, getOperationName(operation.query), {}), takeUntil(teardown$));
63
+ }));
64
+ const forwardedOps$ = pipe(sharedOps$, filter(operation => operation.kind === 'teardown'), forward);
65
+ return merge([executedOps$, forwardedOps$]);
66
+ };
67
+ };
68
+
69
+ export { meshExchange };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@graphql-mesh/urql-exchange",
3
+ "version": "0.0.1-alpha-1ea083c35.0",
4
+ "sideEffects": false,
5
+ "peerDependencies": {
6
+ "@graphql-mesh/runtime": "0.31.5-alpha-1ea083c35.0",
7
+ "@urql/core": "^2.4.3",
8
+ "graphql": "^15.2.0 || ^16.0.0",
9
+ "wonka": "^4.0.15"
10
+ },
11
+ "dependencies": {
12
+ "tslib": "2.3.1"
13
+ },
14
+ "license": "MIT",
15
+ "main": "index.js",
16
+ "module": "index.mjs",
17
+ "typings": "index.d.ts",
18
+ "typescript": {
19
+ "definition": "index.d.ts"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "require": "./index.js",
24
+ "import": "./index.mjs"
25
+ },
26
+ "./*": {
27
+ "require": "./*.js",
28
+ "import": "./*.mjs"
29
+ },
30
+ "./package.json": "./package.json"
31
+ }
32
+ }