Skip to content

Commit

Permalink
Use Promise helpers (#6971)
Browse files Browse the repository at this point in the history
* Use Promise helpers

* chore(dependencies): updated changesets for modified dependencies

* Cleanup

* chore(dependencies): updated changesets for modified dependencies

* Fix Bun unit tests

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] authored Feb 25, 2025
1 parent 68effb5 commit 4a2eb14
Show file tree
Hide file tree
Showing 22 changed files with 175 additions and 360 deletions.
6 changes: 6 additions & 0 deletions .changeset/@graphql-tools_executor-6971-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@graphql-tools/executor": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
- Removed dependency [`value-or-promise@^1.0.12` ↗︎](https://www.npmjs.com/package/value-or-promise/v/1.0.12) (from `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-tools_github-loader-6971-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-tools/github-loader": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-tools_url-loader-6971-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-tools/url-loader": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-tools_utils-6971-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-tools/utils": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
4 changes: 2 additions & 2 deletions packages/executor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"@graphql-typed-document-node/core": "^3.2.0",
"@repeaterjs/repeater": "^3.0.4",
"@whatwg-node/disposablestack": "^0.0.6",
"tslib": "^2.4.0",
"value-or-promise": "^1.0.12"
"@whatwg-node/promise-helpers": "^1.0.0",
"tslib": "^2.4.0"
},
"devDependencies": {
"cross-inspect": "1.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('Abort Signal', () => {
didInvokeFirstFn = true;
return true;
},
second() {
async second() {
didInvokeSecondFn = true;
controller.abort();
return true;
Expand Down
82 changes: 43 additions & 39 deletions packages/executor/src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
TypeNameMetaFieldDef,
versionInfo,
} from 'graphql';
import { ValueOrPromise } from 'value-or-promise';
import {
collectSubFields as _collectSubfields,
addPath,
Expand Down Expand Up @@ -57,6 +56,7 @@ import {
} from '@graphql-tools/utils';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
import { DisposableSymbols } from '@whatwg-node/disposablestack';
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
import { coerceError } from './coerceError.js';
import { flattenAsyncIterable } from './flattenAsyncIterable.js';
import { invariant } from './invariant.js';
Expand Down Expand Up @@ -305,36 +305,33 @@ function executeImpl<TData = any, TVariables = any, TContext = any>(
// Errors from sub-fields of a NonNull type may propagate to the top level,
// at which point we still log the error and null the parent field, which
// in this case is the entire response.
const result = new ValueOrPromise(() => executeOperation<TData, TVariables, TContext>(exeContext))
.then(
data => {
const initialResult = buildResponse(data, exeContext.errors);
if (exeContext.subsequentPayloads.size > 0) {
return {
initialResult: {
...initialResult,
hasNext: true,
},
subsequentResults: yieldSubsequentPayloads(exeContext),
};
}

return initialResult;
},
(error: any) => {
exeContext.signal?.throwIfAborted();
return handleMaybePromise(
() => executeOperation<TData, TVariables, TContext>(exeContext),
data => {
const initialResult = buildResponse(data, exeContext.errors);
if (exeContext.subsequentPayloads.size > 0) {
return {
initialResult: {
...initialResult,
hasNext: true,
},
subsequentResults: yieldSubsequentPayloads(exeContext),
};
}

if (error.errors) {
exeContext.errors.push(...error.errors);
} else {
exeContext.errors.push(error);
}
return buildResponse<TData>(null, exeContext.errors);
},
)
.resolve()!;
return initialResult;
},
(error: any) => {
exeContext.signal?.throwIfAborted();

return result;
if (error.errors) {
exeContext.errors.push(...error.errors);
} else {
exeContext.errors.push(error);
}
return buildResponse<TData>(null, exeContext.errors);
},
);
}

/**
Expand Down Expand Up @@ -575,20 +572,21 @@ function executeFieldsSerially<TData>(
const fieldPath = addPath(path, responseName, parentType.name);
exeContext.signal?.throwIfAborted();

return new ValueOrPromise(() =>
executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath),
).then(result => {
if (result === undefined) {
return results;
}
return handleMaybePromise(
() => executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath),
result => {
if (result === undefined) {
return results;
}

results[responseName] = result;
results[responseName] = result;

return results;
});
return results;
},
);
},
Object.create(null),
).resolve();
);
}

/**
Expand Down Expand Up @@ -1572,6 +1570,12 @@ export function subscribe<TData = any, TVariables = any, TContext = any>(
return mapSourceToResponse(exeContext, resultOrStream);
}

export function isIncrementalResults<TData>(
results: any,
): results is IncrementalExecutionResults<TData> {
return results?.initialResult;
}

export function flattenIncrementalResults<TData>(
incrementalResults: IncrementalExecutionResults<TData>,
): AsyncGenerator<
Expand Down
21 changes: 14 additions & 7 deletions packages/executor/src/execution/normalizedExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getOperationAST, GraphQLSchema } from 'graphql';
import { ValueOrPromise } from 'value-or-promise';
import {
ExecutionRequest,
ExecutionResult,
Expand All @@ -8,7 +7,14 @@ import {
MaybePromise,
memoize1,
} from '@graphql-tools/utils';
import { execute, ExecutionArgs, flattenIncrementalResults, subscribe } from './execute.js';
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
import {
execute,
ExecutionArgs,
flattenIncrementalResults,
isIncrementalResults,
subscribe,
} from './execute.js';

export function normalizedExecutor<TData = any, TVariables = any, TContext = any>(
args: ExecutionArgs<TData, TVariables, TContext>,
Expand All @@ -20,14 +26,15 @@ export function normalizedExecutor<TData = any, TVariables = any, TContext = any
if (operationAST.operation === 'subscription') {
return subscribe(args);
}
return new ValueOrPromise(() => execute(args))
.then((result): MaybeAsyncIterable<ExecutionResult<TData>> => {
if ('initialResult' in result) {
return handleMaybePromise(
() => execute(args),
result => {
if (isIncrementalResults(result)) {
return flattenIncrementalResults(result);
}
return result;
})
.resolve()!;
},
);
}

export const executorFromSchema = memoize1(function executorFromSchema(
Expand Down
1 change: 1 addition & 0 deletions packages/loaders/github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@graphql-tools/graphql-tag-pluck": "^8.3.15",
"@graphql-tools/utils": "^10.8.2",
"@whatwg-node/fetch": "^0.10.0",
"@whatwg-node/promise-helpers": "^1.0.0",
"sync-fetch": "0.6.0-2",
"tslib": "^2.4.0"
},
Expand Down
32 changes: 17 additions & 15 deletions packages/loaders/github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import {
import {
BaseLoaderOptions,
Loader,
mapMaybePromise,
MaybePromise,
parseGraphQLJSON,
parseGraphQLSDL,
Source,
} from '@graphql-tools/utils';
import { fetch as asyncFetch } from '@whatwg-node/fetch';
import { handleMaybePromise } from '@whatwg-node/promise-helpers';

// github:owner/name#ref:path/to/file
function extractData(pointer: string): {
Expand Down Expand Up @@ -96,21 +96,23 @@ export class GithubLoader implements Loader<GithubLoaderOptions> {
return [];
}
const { owner, name, ref, path } = extractData(pointer);
return mapMaybePromise(
mapMaybePromise(
fetchFn(
'https://api.github.com/graphql',
this.prepareRequest({ owner, ref, path, name, options }),
return handleMaybePromise(
() =>
handleMaybePromise(
() =>
fetchFn(
'https://api.github.com/graphql',
this.prepareRequest({ owner, ref, path, name, options }),
),
response => {
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return response.json();
} else {
return response.text();
}
},
),
response => {
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return response.json();
} else {
return response.text();
}
},
),
response => {
const status = response.status;
return this.handleResponse({ pointer, path, options, response, status });
Expand Down
1 change: 1 addition & 0 deletions packages/loaders/url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@graphql-tools/wrap": "^10.0.16",
"@types/ws": "^8.0.0",
"@whatwg-node/fetch": "^0.10.0",
"@whatwg-node/promise-helpers": "^1.0.0",
"isomorphic-ws": "^5.0.0",
"sync-fetch": "0.6.0-2",
"tslib": "^2.4.0",
Expand Down
Loading

0 comments on commit 4a2eb14

Please sign in to comment.