@graphql-tools/executor-urql-exchange 0.0.1-alpha-20221108145549-a9090635

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/cjs/index.js ADDED
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.executorExchange = void 0;
4
+ const wonka_1 = require("wonka");
5
+ const core_1 = require("@urql/core");
6
+ const utils_1 = require("@graphql-tools/utils");
7
+ function executorExchange(executor) {
8
+ function makeYogaSource(operation) {
9
+ const operationName = (0, core_1.getOperationName)(operation.query);
10
+ const extraFetchOptions = typeof operation.context.fetchOptions === 'function'
11
+ ? operation.context.fetchOptions()
12
+ : operation.context.fetchOptions;
13
+ const executionRequest = {
14
+ document: operation.query,
15
+ operationName,
16
+ operationType: operation.kind,
17
+ variables: operation.variables,
18
+ context: operation.context,
19
+ extensions: {
20
+ endpoint: operation.context.url,
21
+ customFetch: operation.context.fetch,
22
+ useGETForQueries: !!operation.context.preferGetMethod,
23
+ headers: extraFetchOptions === null || extraFetchOptions === void 0 ? void 0 : extraFetchOptions.headers,
24
+ method: extraFetchOptions === null || extraFetchOptions === void 0 ? void 0 : extraFetchOptions.method,
25
+ },
26
+ };
27
+ return (0, wonka_1.make)(observer => {
28
+ let ended = false;
29
+ Promise.resolve(executor(executionRequest))
30
+ .then(async (result) => {
31
+ if (ended || !result) {
32
+ return;
33
+ }
34
+ if (!(0, utils_1.isAsyncIterable)(result)) {
35
+ observer.next((0, core_1.makeResult)(operation, result));
36
+ }
37
+ else {
38
+ let prevResult = null;
39
+ for await (const value of result) {
40
+ if (value) {
41
+ prevResult = prevResult ? (0, core_1.mergeResultPatch)(prevResult, value) : (0, core_1.makeResult)(operation, value);
42
+ observer.next(prevResult);
43
+ }
44
+ if (ended) {
45
+ break;
46
+ }
47
+ }
48
+ }
49
+ observer.complete();
50
+ })
51
+ .catch(error => {
52
+ observer.next((0, core_1.makeErrorResult)(operation, error));
53
+ })
54
+ .finally(() => {
55
+ ended = true;
56
+ observer.complete();
57
+ });
58
+ return () => {
59
+ ended = true;
60
+ };
61
+ });
62
+ }
63
+ return function executorExchangeFn({ forward }) {
64
+ return function executorExchangeIO(ops$) {
65
+ const sharedOps$ = (0, wonka_1.share)(ops$);
66
+ const executedOps$ = (0, wonka_1.pipe)(sharedOps$, (0, wonka_1.filter)(operation => operation.kind === 'query' || operation.kind === 'mutation' || operation.kind === 'subscription'), (0, wonka_1.mergeMap)(operation => {
67
+ const teardown$ = (0, wonka_1.pipe)(sharedOps$, (0, wonka_1.filter)(op => op.kind === 'teardown' && op.key === operation.key));
68
+ return (0, wonka_1.pipe)(makeYogaSource(operation), (0, wonka_1.takeUntil)(teardown$));
69
+ }));
70
+ const forwardedOps$ = (0, wonka_1.pipe)(sharedOps$, (0, wonka_1.filter)(operation => operation.kind === 'teardown'), forward);
71
+ return (0, wonka_1.merge)([executedOps$, forwardedOps$]);
72
+ };
73
+ };
74
+ }
75
+ exports.executorExchange = executorExchange;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
package/esm/index.js ADDED
@@ -0,0 +1,71 @@
1
+ import { pipe, share, filter, takeUntil, mergeMap, merge, make } from 'wonka';
2
+ import { makeResult, makeErrorResult, mergeResultPatch, getOperationName, } from '@urql/core';
3
+ import { isAsyncIterable } from '@graphql-tools/utils';
4
+ export function executorExchange(executor) {
5
+ function makeYogaSource(operation) {
6
+ const operationName = getOperationName(operation.query);
7
+ const extraFetchOptions = typeof operation.context.fetchOptions === 'function'
8
+ ? operation.context.fetchOptions()
9
+ : operation.context.fetchOptions;
10
+ const executionRequest = {
11
+ document: operation.query,
12
+ operationName,
13
+ operationType: operation.kind,
14
+ variables: operation.variables,
15
+ context: operation.context,
16
+ extensions: {
17
+ endpoint: operation.context.url,
18
+ customFetch: operation.context.fetch,
19
+ useGETForQueries: !!operation.context.preferGetMethod,
20
+ headers: extraFetchOptions === null || extraFetchOptions === void 0 ? void 0 : extraFetchOptions.headers,
21
+ method: extraFetchOptions === null || extraFetchOptions === void 0 ? void 0 : extraFetchOptions.method,
22
+ },
23
+ };
24
+ return make(observer => {
25
+ let ended = false;
26
+ Promise.resolve(executor(executionRequest))
27
+ .then(async (result) => {
28
+ if (ended || !result) {
29
+ return;
30
+ }
31
+ if (!isAsyncIterable(result)) {
32
+ observer.next(makeResult(operation, result));
33
+ }
34
+ else {
35
+ let prevResult = null;
36
+ for await (const value of result) {
37
+ if (value) {
38
+ prevResult = prevResult ? mergeResultPatch(prevResult, value) : makeResult(operation, value);
39
+ observer.next(prevResult);
40
+ }
41
+ if (ended) {
42
+ break;
43
+ }
44
+ }
45
+ }
46
+ observer.complete();
47
+ })
48
+ .catch(error => {
49
+ observer.next(makeErrorResult(operation, error));
50
+ })
51
+ .finally(() => {
52
+ ended = true;
53
+ observer.complete();
54
+ });
55
+ return () => {
56
+ ended = true;
57
+ };
58
+ });
59
+ }
60
+ return function executorExchangeFn({ forward }) {
61
+ return function executorExchangeIO(ops$) {
62
+ const sharedOps$ = share(ops$);
63
+ const executedOps$ = pipe(sharedOps$, filter(operation => operation.kind === 'query' || operation.kind === 'mutation' || operation.kind === 'subscription'), mergeMap(operation => {
64
+ const teardown$ = pipe(sharedOps$, filter(op => op.kind === 'teardown' && op.key === operation.key));
65
+ return pipe(makeYogaSource(operation), takeUntil(teardown$));
66
+ }));
67
+ const forwardedOps$ = pipe(sharedOps$, filter(operation => operation.kind === 'teardown'), forward);
68
+ return merge([executedOps$, forwardedOps$]);
69
+ };
70
+ };
71
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@graphql-tools/executor-urql-exchange",
3
+ "version": "0.0.1-alpha-20221108145549-a9090635",
4
+ "description": "",
5
+ "peerDependencies": {
6
+ "graphql": "^15.2.0 || ^16.0.0",
7
+ "@urql/core": "^3.0.0",
8
+ "wonka": "^6.0.0"
9
+ },
10
+ "dependencies": {
11
+ "@graphql-tools/utils": "9.1.0",
12
+ "tslib": "^2.4.0"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "ardatan/graphql-tools",
17
+ "directory": "packages/executors/urql-exchange"
18
+ },
19
+ "keywords": [
20
+ "graphql",
21
+ "server",
22
+ "graphql-yoga",
23
+ "apollo"
24
+ ],
25
+ "author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
26
+ "license": "MIT",
27
+ "main": "cjs/index.js",
28
+ "module": "esm/index.js",
29
+ "typings": "typings/index.d.ts",
30
+ "typescript": {
31
+ "definition": "typings/index.d.ts"
32
+ },
33
+ "type": "module",
34
+ "exports": {
35
+ ".": {
36
+ "require": {
37
+ "types": "./typings/index.d.cts",
38
+ "default": "./cjs/index.js"
39
+ },
40
+ "import": {
41
+ "types": "./typings/index.d.ts",
42
+ "default": "./esm/index.js"
43
+ },
44
+ "default": {
45
+ "types": "./typings/index.d.ts",
46
+ "default": "./esm/index.js"
47
+ }
48
+ },
49
+ "./package.json": "./package.json"
50
+ }
51
+ }
@@ -0,0 +1,3 @@
1
+ import { Exchange } from '@urql/core';
2
+ import { Executor } from '@graphql-tools/utils';
3
+ export declare function executorExchange(executor: Executor): Exchange;
@@ -0,0 +1,3 @@
1
+ import { Exchange } from '@urql/core';
2
+ import { Executor } from '@graphql-tools/utils';
3
+ export declare function executorExchange(executor: Executor): Exchange;