@graphql-mesh/urql-exchange 0.0.0-alpha-20220803130803

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