@envelop/apollo-server-errors 1.0.1 → 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 CHANGED
@@ -2,15 +2,46 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
5
  const apolloServerErrors = require('apollo-server-errors');
8
- const isAsyncIterable = _interopDefault(require('graphql/jsutils/isAsyncIterable.js'));
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
+ }
9
36
 
10
37
  const makeHandleResult = (options = {}) => ({ result, setResult }) => {
11
38
  if (result.errors && result.errors.length > 0) {
12
39
  setResult({
13
40
  ...result,
41
+ // Upstream issue in apollo with GraphQL.js 16
42
+ // Type 'ApolloError[]' is not assignable to type 'readonly GraphQLError[]'. Property '[Symbol.toStringTag]' is missing in type 'ApolloError' but required in type 'GraphQLError'.
43
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
44
+ // @ts-ignore
14
45
  errors: apolloServerErrors.formatApolloErrors(result.errors, {
15
46
  debug: options.debug,
16
47
  formatter: options.formatter,
@@ -23,16 +54,8 @@ const useApolloServerErrors = (options = {}) => {
23
54
  onExecute() {
24
55
  const handleResult = makeHandleResult(options);
25
56
  return {
26
- onExecuteDone({ result, setResult }) {
27
- if (isAsyncIterable(result)) {
28
- return {
29
- onNext: ({ result, setResult }) => {
30
- handleResult({ result, setResult });
31
- },
32
- };
33
- }
34
- handleResult({ result, setResult });
35
- return undefined;
57
+ onExecuteDone(payload) {
58
+ return handleStreamOrSingleExecutionResult(payload, handleResult);
36
59
  },
37
60
  };
38
61
  },
@@ -40,4 +63,3 @@ const useApolloServerErrors = (options = {}) => {
40
63
  };
41
64
 
42
65
  exports.useApolloServerErrors = useApolloServerErrors;
43
- //# sourceMappingURL=index.js.map
package/index.mjs CHANGED
@@ -1,10 +1,43 @@
1
1
  import { formatApolloErrors } from 'apollo-server-errors';
2
- import isAsyncIterable from 'graphql/jsutils/isAsyncIterable.js';
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
+ }
3
32
 
4
33
  const makeHandleResult = (options = {}) => ({ result, setResult }) => {
5
34
  if (result.errors && result.errors.length > 0) {
6
35
  setResult({
7
36
  ...result,
37
+ // Upstream issue in apollo with GraphQL.js 16
38
+ // Type 'ApolloError[]' is not assignable to type 'readonly GraphQLError[]'. Property '[Symbol.toStringTag]' is missing in type 'ApolloError' but required in type 'GraphQLError'.
39
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
40
+ // @ts-ignore
8
41
  errors: formatApolloErrors(result.errors, {
9
42
  debug: options.debug,
10
43
  formatter: options.formatter,
@@ -17,16 +50,8 @@ const useApolloServerErrors = (options = {}) => {
17
50
  onExecute() {
18
51
  const handleResult = makeHandleResult(options);
19
52
  return {
20
- onExecuteDone({ result, setResult }) {
21
- if (isAsyncIterable(result)) {
22
- return {
23
- onNext: ({ result, setResult }) => {
24
- handleResult({ result, setResult });
25
- },
26
- };
27
- }
28
- handleResult({ result, setResult });
29
- return undefined;
53
+ onExecuteDone(payload) {
54
+ return handleStreamOrSingleExecutionResult(payload, handleResult);
30
55
  },
31
56
  };
32
57
  },
@@ -34,4 +59,3 @@ const useApolloServerErrors = (options = {}) => {
34
59
  };
35
60
 
36
61
  export { useApolloServerErrors };
37
- //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@envelop/apollo-server-errors",
3
- "version": "1.0.1",
3
+ "version": "1.1.0-alpha-e8998c9.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "graphql": "^14.0.0 || ^15.0.0"
6
+ "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
7
7
  },
8
8
  "dependencies": {
9
- "apollo-server-errors": "3.0.1"
9
+ "apollo-server-errors": "3.2.0"
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
package/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../../../dist/plugins/apollo-server-errors/src/index.js"],"sourcesContent":["import { formatApolloErrors } from 'apollo-server-errors';\nimport isAsyncIterable from 'graphql/jsutils/isAsyncIterable.js';\nconst makeHandleResult = (options = {}) => ({ result, setResult }) => {\n if (result.errors && result.errors.length > 0) {\n setResult({\n ...result,\n errors: formatApolloErrors(result.errors, {\n debug: options.debug,\n formatter: options.formatter,\n }),\n });\n }\n};\nexport const useApolloServerErrors = (options = {}) => {\n return {\n onExecute() {\n const handleResult = makeHandleResult(options);\n return {\n onExecuteDone({ result, setResult }) {\n if (isAsyncIterable(result)) {\n return {\n onNext: ({ result, setResult }) => {\n handleResult({ result, setResult });\n },\n };\n }\n handleResult({ result, setResult });\n return undefined;\n },\n };\n },\n };\n};\n//# sourceMappingURL=index.js.map"],"names":["formatApolloErrors"],"mappings":";;;;;;;;;AAEA,MAAM,gBAAgB,GAAG,CAAC,OAAO,GAAG,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK;AACtE,IAAI,IAAI,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;AACnD,QAAQ,SAAS,CAAC;AAClB,YAAY,GAAG,MAAM;AACrB,YAAY,MAAM,EAAEA,qCAAkB,CAAC,MAAM,CAAC,MAAM,EAAE;AACtD,gBAAgB,KAAK,EAAE,OAAO,CAAC,KAAK;AACpC,gBAAgB,SAAS,EAAE,OAAO,CAAC,SAAS;AAC5C,aAAa,CAAC;AACd,SAAS,CAAC,CAAC;AACX,KAAK;AACL,CAAC,CAAC;AACU,MAAC,qBAAqB,GAAG,CAAC,OAAO,GAAG,EAAE,KAAK;AACvD,IAAI,OAAO;AACX,QAAQ,SAAS,GAAG;AACpB,YAAY,MAAM,YAAY,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;AAC3D,YAAY,OAAO;AACnB,gBAAgB,aAAa,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE;AACrD,oBAAoB,IAAI,eAAe,CAAC,MAAM,CAAC,EAAE;AACjD,wBAAwB,OAAO;AAC/B,4BAA4B,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK;AAC/D,gCAAgC,YAAY,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;AACpE,6BAA6B;AAC7B,yBAAyB,CAAC;AAC1B,qBAAqB;AACrB,oBAAoB,YAAY,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;AACxD,oBAAoB,OAAO,SAAS,CAAC;AACrC,iBAAiB;AACjB,aAAa,CAAC;AACd,SAAS;AACT,KAAK,CAAC;AACN;;;;"}
package/index.mjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.mjs","sources":["../../../dist/plugins/apollo-server-errors/src/index.js"],"sourcesContent":["import { formatApolloErrors } from 'apollo-server-errors';\nimport isAsyncIterable from 'graphql/jsutils/isAsyncIterable.js';\nconst makeHandleResult = (options = {}) => ({ result, setResult }) => {\n if (result.errors && result.errors.length > 0) {\n setResult({\n ...result,\n errors: formatApolloErrors(result.errors, {\n debug: options.debug,\n formatter: options.formatter,\n }),\n });\n }\n};\nexport const useApolloServerErrors = (options = {}) => {\n return {\n onExecute() {\n const handleResult = makeHandleResult(options);\n return {\n onExecuteDone({ result, setResult }) {\n if (isAsyncIterable(result)) {\n return {\n onNext: ({ result, setResult }) => {\n handleResult({ result, setResult });\n },\n };\n }\n handleResult({ result, setResult });\n return undefined;\n },\n };\n },\n };\n};\n//# sourceMappingURL=index.js.map"],"names":[],"mappings":";;;AAEA,MAAM,gBAAgB,GAAG,CAAC,OAAO,GAAG,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK;AACtE,IAAI,IAAI,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;AACnD,QAAQ,SAAS,CAAC;AAClB,YAAY,GAAG,MAAM;AACrB,YAAY,MAAM,EAAE,kBAAkB,CAAC,MAAM,CAAC,MAAM,EAAE;AACtD,gBAAgB,KAAK,EAAE,OAAO,CAAC,KAAK;AACpC,gBAAgB,SAAS,EAAE,OAAO,CAAC,SAAS;AAC5C,aAAa,CAAC;AACd,SAAS,CAAC,CAAC;AACX,KAAK;AACL,CAAC,CAAC;AACU,MAAC,qBAAqB,GAAG,CAAC,OAAO,GAAG,EAAE,KAAK;AACvD,IAAI,OAAO;AACX,QAAQ,SAAS,GAAG;AACpB,YAAY,MAAM,YAAY,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;AAC3D,YAAY,OAAO;AACnB,gBAAgB,aAAa,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE;AACrD,oBAAoB,IAAI,eAAe,CAAC,MAAM,CAAC,EAAE;AACjD,wBAAwB,OAAO;AAC/B,4BAA4B,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK;AAC/D,gCAAgC,YAAY,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;AACpE,6BAA6B;AAC7B,yBAAyB,CAAC;AAC1B,qBAAqB;AACrB,oBAAoB,YAAY,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;AACxD,oBAAoB,OAAO,SAAS,CAAC;AACrC,iBAAiB;AACjB,aAAa,CAAC;AACd,SAAS;AACT,KAAK,CAAC;AACN;;;;"}