@envelop/apollo-server-errors 1.1.0-alpha-d7e3951.0 → 1.1.0-alpha-e8998c9.0
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.js +31 -2
- package/index.mjs +30 -1
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -2,9 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const types = require('@envelop/types');
|
|
6
5
|
const apolloServerErrors = require('apollo-server-errors');
|
|
7
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Returns true if the provided object implements the AsyncIterator protocol via
|
|
9
|
+
* implementing a `Symbol.asyncIterator` method.
|
|
10
|
+
*
|
|
11
|
+
* Source: https://github.com/graphql/graphql-js/blob/main/src/jsutils/isAsyncIterable.ts
|
|
12
|
+
*/
|
|
13
|
+
function isAsyncIterable(maybeAsyncIterable) {
|
|
14
|
+
return typeof (maybeAsyncIterable === null || maybeAsyncIterable === void 0 ? void 0 : maybeAsyncIterable[Symbol.asyncIterator]) === 'function';
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A utility function for hanlding `onExecuteDone` hook result, for simplifying the hanlding of AsyncIterable returned from `execute`.
|
|
18
|
+
*
|
|
19
|
+
* @param payload The payload send to `onExecuteDone` hook function
|
|
20
|
+
* @param fn The handler to be executed on each result
|
|
21
|
+
* @returns a subscription for streamed results, or undefined in case of an non-async
|
|
22
|
+
*/
|
|
23
|
+
function handleStreamOrSingleExecutionResult(payload, fn) {
|
|
24
|
+
if (isAsyncIterable(payload.result)) {
|
|
25
|
+
return { onNext: fn };
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
fn({
|
|
29
|
+
args: payload.args,
|
|
30
|
+
result: payload.result,
|
|
31
|
+
setResult: payload.setResult,
|
|
32
|
+
});
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
8
37
|
const makeHandleResult = (options = {}) => ({ result, setResult }) => {
|
|
9
38
|
if (result.errors && result.errors.length > 0) {
|
|
10
39
|
setResult({
|
|
@@ -26,7 +55,7 @@ const useApolloServerErrors = (options = {}) => {
|
|
|
26
55
|
const handleResult = makeHandleResult(options);
|
|
27
56
|
return {
|
|
28
57
|
onExecuteDone(payload) {
|
|
29
|
-
return
|
|
58
|
+
return handleStreamOrSingleExecutionResult(payload, handleResult);
|
|
30
59
|
},
|
|
31
60
|
};
|
|
32
61
|
},
|
package/index.mjs
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
|
-
import { handleStreamOrSingleExecutionResult } from '@envelop/types';
|
|
2
1
|
import { formatApolloErrors } from 'apollo-server-errors';
|
|
3
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Returns true if the provided object implements the AsyncIterator protocol via
|
|
5
|
+
* implementing a `Symbol.asyncIterator` method.
|
|
6
|
+
*
|
|
7
|
+
* Source: https://github.com/graphql/graphql-js/blob/main/src/jsutils/isAsyncIterable.ts
|
|
8
|
+
*/
|
|
9
|
+
function isAsyncIterable(maybeAsyncIterable) {
|
|
10
|
+
return typeof (maybeAsyncIterable === null || maybeAsyncIterable === void 0 ? void 0 : maybeAsyncIterable[Symbol.asyncIterator]) === 'function';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A utility function for hanlding `onExecuteDone` hook result, for simplifying the hanlding of AsyncIterable returned from `execute`.
|
|
14
|
+
*
|
|
15
|
+
* @param payload The payload send to `onExecuteDone` hook function
|
|
16
|
+
* @param fn The handler to be executed on each result
|
|
17
|
+
* @returns a subscription for streamed results, or undefined in case of an non-async
|
|
18
|
+
*/
|
|
19
|
+
function handleStreamOrSingleExecutionResult(payload, fn) {
|
|
20
|
+
if (isAsyncIterable(payload.result)) {
|
|
21
|
+
return { onNext: fn };
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
fn({
|
|
25
|
+
args: payload.args,
|
|
26
|
+
result: payload.result,
|
|
27
|
+
setResult: payload.setResult,
|
|
28
|
+
});
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
4
33
|
const makeHandleResult = (options = {}) => ({ result, setResult }) => {
|
|
5
34
|
if (result.errors && result.errors.length > 0) {
|
|
6
35
|
setResult({
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/apollo-server-errors",
|
|
3
|
-
"version": "1.1.0-alpha-
|
|
3
|
+
"version": "1.1.0-alpha-e8998c9.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"apollo-server-errors": "3.
|
|
9
|
+
"apollo-server-errors": "3.2.0"
|
|
10
10
|
},
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|