Skip to content

Commit

Permalink
Move the merging logic of incremental results to the utils package (#…
Browse files Browse the repository at this point in the history
…5396)

* Move the merging logic of incremental results to the utils package

* chore(dependencies): updated changesets for modified dependencies

* More

* Simplify GraphQL WS excutor

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] authored Jul 3, 2023
1 parent f1ca215 commit bb8f169
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 83 deletions.
5 changes: 5 additions & 0 deletions .changeset/@graphql-tools_executor-http-5396-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/executor-http': patch
---
dependencies updates:
- Removed dependency [`dset@^3.1.2` ↗︎](https://www.npmjs.com/package/dset/v/3.1.2) (from `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-tools_utils-5396-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': patch
---
dependencies updates:
- Added dependency [`dset@^3.1.2` ↗︎](https://www.npmjs.com/package/dset/v/3.1.2) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/dry-seahorses-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/executor-graphql-ws': patch
---

Simplify GraphQL WS executor
6 changes: 6 additions & 0 deletions .changeset/young-lies-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-tools/executor-http': patch
'@graphql-tools/utils': patch
---

Move the merging logic of incremental results to the utils package
7 changes: 3 additions & 4 deletions packages/executors/graphql-ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@
},
"dependencies": {
"@types/ws": "^8.0.0",
"ws": "8.13.0",
"isomorphic-ws": "5.0.0",
"ws": "^8.13.0",
"isomorphic-ws": "^5.0.0",
"@graphql-tools/utils": "^10.0.0",
"@repeaterjs/repeater": "3.0.4",
"graphql-ws": "5.14.0",
"graphql-ws": "^5.14.0",
"tslib": "^2.4.0"
},
"publishConfig": {
Expand Down
54 changes: 9 additions & 45 deletions packages/executors/graphql-ws/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ExecutionRequest, Executor, ExecutionResult, getOperationASTFromRequest } from '@graphql-tools/utils';
import { Repeater } from '@repeaterjs/repeater';
import { print } from 'graphql';
import { Client, ClientOptions, createClient } from 'graphql-ws';
import WebSocket from 'isomorphic-ws';
Expand Down Expand Up @@ -33,7 +32,7 @@ export function buildGraphQLWSExecutor(clientOptionsOrClient: GraphQLWSExecutorO
TExtensions extends Record<string, any>
>(
executionRequest: ExecutionRequest<TArgs, any, TRoot, TExtensions>
): Repeater<ExecutionResult<TData>> | Promise<ExecutionResult<TData>> {
): AsyncIterableIterator<ExecutionResult<TData>> | Promise<ExecutionResult<TData>> {
const {
document,
variables,
Expand All @@ -42,50 +41,15 @@ export function buildGraphQLWSExecutor(clientOptionsOrClient: GraphQLWSExecutorO
operationType = getOperationASTFromRequest(executionRequest).operation,
} = executionRequest;
const query = print(document);
const iterableIterator = graphqlWSClient.iterate<TData, TExtensions>({
query,
variables,
operationName,
extensions,
});
if (operationType === 'subscription') {
return new Repeater(function repeaterExecutor(push, stop) {
const unsubscribe = graphqlWSClient.subscribe<TData, TExtensions>(
{
query,
variables,
operationName,
extensions,
},
{
next(data) {
return push(data);
},
error(error) {
return stop(error);
},
complete() {
return stop();
},
}
);
return stop.finally(unsubscribe);
});
return iterableIterator;
}
return new Promise((resolve, reject) => {
const unsubscribe = graphqlWSClient.subscribe<TData, TExtensions>(
{
query,
variables,
operationName,
extensions,
},
{
next(data) {
return resolve(data);
},
error(error) {
return reject(error);
},
complete() {
unsubscribe();
},
}
);
});
return iterableIterator.next().then(({ value }) => value);
};
}
1 change: 0 additions & 1 deletion packages/executors/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"value-or-promise": "^1.0.12",
"@whatwg-node/fetch": "^0.9.0",
"@repeaterjs/repeater": "^3.0.4",
"dset": "^3.1.2",
"meros": "^1.2.1",
"extract-files": "^11.0.0",
"tslib": "^2.4.0"
Expand Down
36 changes: 6 additions & 30 deletions packages/executors/http/src/handleMultipartMixedResponse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { IncomingMessage } from 'http';
import { meros as merosIncomingMessage } from 'meros/node';
import { meros as merosReadableStream } from 'meros/browser';
import { ExecutionResult, mapAsyncIterator } from '@graphql-tools/utils';
import { dset } from 'dset/merge';
import { ExecutionResult, mapAsyncIterator, mergeIncrementalResult } from '@graphql-tools/utils';
import { addCancelToResponseStream } from './addCancelToResponseStream.js';
import { GraphQLError } from 'graphql';

type Part =
| {
Expand Down Expand Up @@ -44,39 +42,17 @@ export async function handleMultipartMixedResponse(response: Response, controlle

const executionResult: ExecutionResult = {};

function handleResult(result: ExecutionResult) {
if (result.path) {
const path = ['data', ...result.path];
executionResult.data = executionResult.data || {};
if (result.items) {
for (const item of result.items) {
dset(executionResult, path, item);
}
}
if (result.data) {
dset(executionResult, ['data', ...result.path], result.data);
}
} else if (result.data) {
executionResult.data = executionResult.data || {};
Object.assign(executionResult.data, result.data);
}
if (result.errors) {
executionResult.errors = executionResult.errors || [];
(executionResult.errors as GraphQLError[]).push(...result.errors);
}
if (result.incremental) {
result.incremental.forEach(handleResult);
}
}

if (asyncIterator == null) {
return executionResult;
}

const resultStream = mapAsyncIterator(asyncIterator, (part: Part) => {
if (part.json) {
const chunk = part.body;
handleResult(chunk);
const incrementalResult = part.body;
mergeIncrementalResult({
incrementalResult,
executionResult,
});
return executionResult;
}
});
Expand Down
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
},
"dependencies": {
"@graphql-typed-document-node/core": "^3.1.1",
"dset": "^3.1.2",
"tslib": "^2.4.0"
},
"publishConfig": {
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ export * from './extractExtensionsFromSchema.js';
export * from './Path.js';
export * from './jsutils.js';
export * from './directives.js';
export * from './mergeIncrementalResult.js';
39 changes: 39 additions & 0 deletions packages/utils/src/mergeIncrementalResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { GraphQLError } from 'graphql';
import { dset } from 'dset/merge';
import { ExecutionResult } from './Interfaces.js';

export function mergeIncrementalResult({
incrementalResult,
executionResult,
}: {
incrementalResult: ExecutionResult;
executionResult: ExecutionResult;
}) {
if (incrementalResult.path) {
const path = ['data', ...incrementalResult.path];
executionResult.data = executionResult.data || {};
if (incrementalResult.items) {
for (const item of incrementalResult.items) {
dset(executionResult, path, item);
}
}
if (incrementalResult.data) {
dset(executionResult, ['data', ...incrementalResult.path], incrementalResult.data);
}
} else if (incrementalResult.data) {
executionResult.data = executionResult.data || {};
Object.assign(executionResult.data, incrementalResult.data);
}
if (incrementalResult.errors) {
executionResult.errors = executionResult.errors || [];
(executionResult.errors as GraphQLError[]).push(...executionResult.errors);
}
if (incrementalResult.incremental) {
incrementalResult.incremental.forEach(incrementalSubResult => {
mergeIncrementalResult({
incrementalResult: incrementalSubResult,
executionResult,
});
});
}
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2646,7 +2646,7 @@
"@babel/runtime" "^7.13.10"
"@radix-ui/react-primitive" "1.0.3"

"@repeaterjs/repeater@3.0.4", "@repeaterjs/repeater@^3.0.4":
"@repeaterjs/repeater@^3.0.4":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@repeaterjs/repeater/-/repeater-3.0.4.tgz#a04d63f4d1bf5540a41b01a921c9a7fddc3bd1ca"
integrity sha512-AW8PKd6iX3vAZ0vA43nOUOnbq/X5ihgU+mSXXqunMkeQADGiqw/PY0JNeYtD5sr0PAy51YPgAPbDoeapv9r8WA==
Expand Down Expand Up @@ -6657,7 +6657,7 @@ graphql-upload@16.0.2:
http-errors "^2.0.0"
object-path "^0.11.8"

graphql-ws@5.14.0:
graphql-ws@^5.14.0:
version "5.14.0"
resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-5.14.0.tgz#766f249f3974fc2c48fae0d1fb20c2c4c79cd591"
integrity sha512-itrUTQZP/TgswR4GSSYuwWUzrE/w5GhbwM2GX3ic2U7aw33jgEsayfIlvaj7/GcIvZgNMzsPTrE5hqPuFUiE5g==
Expand Down Expand Up @@ -12298,7 +12298,7 @@ write-file-atomic@^4.0.2:
imurmurhash "^0.1.4"
signal-exit "^3.0.7"

ws@8.13.0, ws@^8.12.0:
ws@8.13.0, ws@^8.12.0, ws@^8.13.0:
version "8.13.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0"
integrity sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==
Expand Down

0 comments on commit bb8f169

Please sign in to comment.