@envelop/apollo-server-errors 1.0.2 → 1.1.0-alpha-ed2d5ef.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,13 +2,48 @@
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 (maybeAsyncIterable != null &&
15
+ typeof maybeAsyncIterable === 'object' &&
16
+ typeof maybeAsyncIterable[Symbol.asyncIterator] === 'function');
17
+ }
18
+ /**
19
+ * A utility function for hanlding `onExecuteDone` hook result, for simplifying the hanlding of AsyncIterable returned from `execute`.
20
+ *
21
+ * @param payload The payload send to `onExecuteDone` hook function
22
+ * @param fn The handler to be executed on each result
23
+ * @returns a subscription for streamed results, or undefined in case of an non-async
24
+ */
25
+ function handleStreamOrSingleExecutionResult(payload, fn) {
26
+ if (isAsyncIterable(payload.result)) {
27
+ return { onNext: fn };
28
+ }
29
+ else {
30
+ fn({
31
+ args: payload.args,
32
+ result: payload.result,
33
+ setResult: payload.setResult,
34
+ });
35
+ return undefined;
36
+ }
37
+ }
38
+
8
39
  const makeHandleResult = (options = {}) => ({ result, setResult }) => {
9
40
  if (result.errors && result.errors.length > 0) {
10
41
  setResult({
11
42
  ...result,
43
+ // Upstream issue in apollo with GraphQL.js 16
44
+ // Type 'ApolloError[]' is not assignable to type 'readonly GraphQLError[]'. Property '[Symbol.toStringTag]' is missing in type 'ApolloError' but required in type 'GraphQLError'.
45
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
46
+ // @ts-ignore
12
47
  errors: apolloServerErrors.formatApolloErrors(result.errors, {
13
48
  debug: options.debug,
14
49
  formatter: options.formatter,
@@ -22,7 +57,7 @@ const useApolloServerErrors = (options = {}) => {
22
57
  const handleResult = makeHandleResult(options);
23
58
  return {
24
59
  onExecuteDone(payload) {
25
- return types.handleStreamOrSingleExecutionResult(payload, handleResult);
60
+ return handleStreamOrSingleExecutionResult(payload, handleResult);
26
61
  },
27
62
  };
28
63
  },
@@ -30,4 +65,3 @@ const useApolloServerErrors = (options = {}) => {
30
65
  };
31
66
 
32
67
  exports.useApolloServerErrors = useApolloServerErrors;
33
- //# sourceMappingURL=index.js.map
package/index.mjs CHANGED
@@ -1,10 +1,45 @@
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 (maybeAsyncIterable != null &&
11
+ typeof maybeAsyncIterable === 'object' &&
12
+ typeof maybeAsyncIterable[Symbol.asyncIterator] === 'function');
13
+ }
14
+ /**
15
+ * A utility function for hanlding `onExecuteDone` hook result, for simplifying the hanlding of AsyncIterable returned from `execute`.
16
+ *
17
+ * @param payload The payload send to `onExecuteDone` hook function
18
+ * @param fn The handler to be executed on each result
19
+ * @returns a subscription for streamed results, or undefined in case of an non-async
20
+ */
21
+ function handleStreamOrSingleExecutionResult(payload, fn) {
22
+ if (isAsyncIterable(payload.result)) {
23
+ return { onNext: fn };
24
+ }
25
+ else {
26
+ fn({
27
+ args: payload.args,
28
+ result: payload.result,
29
+ setResult: payload.setResult,
30
+ });
31
+ return undefined;
32
+ }
33
+ }
34
+
4
35
  const makeHandleResult = (options = {}) => ({ result, setResult }) => {
5
36
  if (result.errors && result.errors.length > 0) {
6
37
  setResult({
7
38
  ...result,
39
+ // Upstream issue in apollo with GraphQL.js 16
40
+ // Type 'ApolloError[]' is not assignable to type 'readonly GraphQLError[]'. Property '[Symbol.toStringTag]' is missing in type 'ApolloError' but required in type 'GraphQLError'.
41
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
42
+ // @ts-ignore
8
43
  errors: formatApolloErrors(result.errors, {
9
44
  debug: options.debug,
10
45
  formatter: options.formatter,
@@ -26,4 +61,3 @@ const useApolloServerErrors = (options = {}) => {
26
61
  };
27
62
 
28
63
  export { useApolloServerErrors };
29
- //# 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.2",
3
+ "version": "1.1.0-alpha-ed2d5ef.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 { handleStreamOrSingleExecutionResult } from '@envelop/types';\nimport { formatApolloErrors } from 'apollo-server-errors';\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(payload) {\n return handleStreamOrSingleExecutionResult(payload, handleResult);\n },\n };\n },\n };\n};\n//# sourceMappingURL=index.js.map"],"names":["formatApolloErrors","handleStreamOrSingleExecutionResult"],"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,OAAO,EAAE;AACvC,oBAAoB,OAAOC,yCAAmC,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;AACtF,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 { handleStreamOrSingleExecutionResult } from '@envelop/types';\nimport { formatApolloErrors } from 'apollo-server-errors';\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(payload) {\n return handleStreamOrSingleExecutionResult(payload, handleResult);\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,OAAO,EAAE;AACvC,oBAAoB,OAAO,mCAAmC,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;AACtF,iBAAiB;AACjB,aAAa,CAAC;AACd,SAAS;AACT,KAAK,CAAC;AACN;;;;"}