Skip to content

Commit 4a2eb14

Browse files
Use Promise helpers (#6971)
* 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>
1 parent 68effb5 commit 4a2eb14

22 files changed

+175
-360
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphql-tools/executor": patch
3+
---
4+
dependencies updates:
5+
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
6+
- Removed dependency [`value-or-promise@^1.0.12` ↗︎](https://www.npmjs.com/package/value-or-promise/v/1.0.12) (from `dependencies`)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-tools/github-loader": patch
3+
---
4+
dependencies updates:
5+
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-tools/url-loader": patch
3+
---
4+
dependencies updates:
5+
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-tools/utils": patch
3+
---
4+
dependencies updates:
5+
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)

packages/executor/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
"@graphql-typed-document-node/core": "^3.2.0",
6060
"@repeaterjs/repeater": "^3.0.4",
6161
"@whatwg-node/disposablestack": "^0.0.6",
62-
"tslib": "^2.4.0",
63-
"value-or-promise": "^1.0.12"
62+
"@whatwg-node/promise-helpers": "^1.0.0",
63+
"tslib": "^2.4.0"
6464
},
6565
"devDependencies": {
6666
"cross-inspect": "1.0.1",

packages/executor/src/execution/__tests__/abort-signal.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('Abort Signal', () => {
150150
didInvokeFirstFn = true;
151151
return true;
152152
},
153-
second() {
153+
async second() {
154154
didInvokeSecondFn = true;
155155
controller.abort();
156156
return true;

packages/executor/src/execution/execute.ts

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
TypeNameMetaFieldDef,
2929
versionInfo,
3030
} from 'graphql';
31-
import { ValueOrPromise } from 'value-or-promise';
3231
import {
3332
collectSubFields as _collectSubfields,
3433
addPath,
@@ -57,6 +56,7 @@ import {
5756
} from '@graphql-tools/utils';
5857
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
5958
import { DisposableSymbols } from '@whatwg-node/disposablestack';
59+
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
6060
import { coerceError } from './coerceError.js';
6161
import { flattenAsyncIterable } from './flattenAsyncIterable.js';
6262
import { invariant } from './invariant.js';
@@ -305,36 +305,33 @@ function executeImpl<TData = any, TVariables = any, TContext = any>(
305305
// Errors from sub-fields of a NonNull type may propagate to the top level,
306306
// at which point we still log the error and null the parent field, which
307307
// in this case is the entire response.
308-
const result = new ValueOrPromise(() => executeOperation<TData, TVariables, TContext>(exeContext))
309-
.then(
310-
data => {
311-
const initialResult = buildResponse(data, exeContext.errors);
312-
if (exeContext.subsequentPayloads.size > 0) {
313-
return {
314-
initialResult: {
315-
...initialResult,
316-
hasNext: true,
317-
},
318-
subsequentResults: yieldSubsequentPayloads(exeContext),
319-
};
320-
}
321-
322-
return initialResult;
323-
},
324-
(error: any) => {
325-
exeContext.signal?.throwIfAborted();
308+
return handleMaybePromise(
309+
() => executeOperation<TData, TVariables, TContext>(exeContext),
310+
data => {
311+
const initialResult = buildResponse(data, exeContext.errors);
312+
if (exeContext.subsequentPayloads.size > 0) {
313+
return {
314+
initialResult: {
315+
...initialResult,
316+
hasNext: true,
317+
},
318+
subsequentResults: yieldSubsequentPayloads(exeContext),
319+
};
320+
}
326321

327-
if (error.errors) {
328-
exeContext.errors.push(...error.errors);
329-
} else {
330-
exeContext.errors.push(error);
331-
}
332-
return buildResponse<TData>(null, exeContext.errors);
333-
},
334-
)
335-
.resolve()!;
322+
return initialResult;
323+
},
324+
(error: any) => {
325+
exeContext.signal?.throwIfAborted();
336326

337-
return result;
327+
if (error.errors) {
328+
exeContext.errors.push(...error.errors);
329+
} else {
330+
exeContext.errors.push(error);
331+
}
332+
return buildResponse<TData>(null, exeContext.errors);
333+
},
334+
);
338335
}
339336

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

578-
return new ValueOrPromise(() =>
579-
executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath),
580-
).then(result => {
581-
if (result === undefined) {
582-
return results;
583-
}
575+
return handleMaybePromise(
576+
() => executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath),
577+
result => {
578+
if (result === undefined) {
579+
return results;
580+
}
584581

585-
results[responseName] = result;
582+
results[responseName] = result;
586583

587-
return results;
588-
});
584+
return results;
585+
},
586+
);
589587
},
590588
Object.create(null),
591-
).resolve();
589+
);
592590
}
593591

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

1573+
export function isIncrementalResults<TData>(
1574+
results: any,
1575+
): results is IncrementalExecutionResults<TData> {
1576+
return results?.initialResult;
1577+
}
1578+
15751579
export function flattenIncrementalResults<TData>(
15761580
incrementalResults: IncrementalExecutionResults<TData>,
15771581
): AsyncGenerator<

packages/executor/src/execution/normalizedExecutor.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getOperationAST, GraphQLSchema } from 'graphql';
2-
import { ValueOrPromise } from 'value-or-promise';
32
import {
43
ExecutionRequest,
54
ExecutionResult,
@@ -8,7 +7,14 @@ import {
87
MaybePromise,
98
memoize1,
109
} from '@graphql-tools/utils';
11-
import { execute, ExecutionArgs, flattenIncrementalResults, subscribe } from './execute.js';
10+
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
11+
import {
12+
execute,
13+
ExecutionArgs,
14+
flattenIncrementalResults,
15+
isIncrementalResults,
16+
subscribe,
17+
} from './execute.js';
1218

1319
export function normalizedExecutor<TData = any, TVariables = any, TContext = any>(
1420
args: ExecutionArgs<TData, TVariables, TContext>,
@@ -20,14 +26,15 @@ export function normalizedExecutor<TData = any, TVariables = any, TContext = any
2026
if (operationAST.operation === 'subscription') {
2127
return subscribe(args);
2228
}
23-
return new ValueOrPromise(() => execute(args))
24-
.then((result): MaybeAsyncIterable<ExecutionResult<TData>> => {
25-
if ('initialResult' in result) {
29+
return handleMaybePromise(
30+
() => execute(args),
31+
result => {
32+
if (isIncrementalResults(result)) {
2633
return flattenIncrementalResults(result);
2734
}
2835
return result;
29-
})
30-
.resolve()!;
36+
},
37+
);
3138
}
3239

3340
export const executorFromSchema = memoize1(function executorFromSchema(

packages/loaders/github/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@graphql-tools/graphql-tag-pluck": "^8.3.15",
5656
"@graphql-tools/utils": "^10.8.2",
5757
"@whatwg-node/fetch": "^0.10.0",
58+
"@whatwg-node/promise-helpers": "^1.0.0",
5859
"sync-fetch": "0.6.0-2",
5960
"tslib": "^2.4.0"
6061
},

packages/loaders/github/src/index.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import {
88
import {
99
BaseLoaderOptions,
1010
Loader,
11-
mapMaybePromise,
1211
MaybePromise,
1312
parseGraphQLJSON,
1413
parseGraphQLSDL,
1514
Source,
1615
} from '@graphql-tools/utils';
1716
import { fetch as asyncFetch } from '@whatwg-node/fetch';
17+
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
1818

1919
// github:owner/name#ref:path/to/file
2020
function extractData(pointer: string): {
@@ -96,21 +96,23 @@ export class GithubLoader implements Loader<GithubLoaderOptions> {
9696
return [];
9797
}
9898
const { owner, name, ref, path } = extractData(pointer);
99-
return mapMaybePromise(
100-
mapMaybePromise(
101-
fetchFn(
102-
'https://api.github.com/graphql',
103-
this.prepareRequest({ owner, ref, path, name, options }),
99+
return handleMaybePromise(
100+
() =>
101+
handleMaybePromise(
102+
() =>
103+
fetchFn(
104+
'https://api.github.com/graphql',
105+
this.prepareRequest({ owner, ref, path, name, options }),
106+
),
107+
response => {
108+
const contentType = response.headers.get('content-type');
109+
if (contentType && contentType.includes('application/json')) {
110+
return response.json();
111+
} else {
112+
return response.text();
113+
}
114+
},
104115
),
105-
response => {
106-
const contentType = response.headers.get('content-type');
107-
if (contentType && contentType.includes('application/json')) {
108-
return response.json();
109-
} else {
110-
return response.text();
111-
}
112-
},
113-
),
114116
response => {
115117
const status = response.status;
116118
return this.handleResponse({ pointer, path, options, response, status });

packages/loaders/url/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@graphql-tools/wrap": "^10.0.16",
5959
"@types/ws": "^8.0.0",
6060
"@whatwg-node/fetch": "^0.10.0",
61+
"@whatwg-node/promise-helpers": "^1.0.0",
6162
"isomorphic-ws": "^5.0.0",
6263
"sync-fetch": "0.6.0-2",
6364
"tslib": "^2.4.0",

0 commit comments

Comments
 (0)