@graphql-tools/batch-execute 9.0.1 → 9.0.2-rc-20230823115638-58a6e1fa

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/cjs/prefix.js CHANGED
@@ -1,16 +1,42 @@
1
1
  "use strict";
2
2
  // adapted from https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.parseKey = exports.createPrefix = void 0;
4
+ exports.parseKeyFromPath = exports.parseKey = exports.createPrefix = void 0;
5
5
  function createPrefix(index) {
6
6
  return `_${index}_`;
7
7
  }
8
8
  exports.createPrefix = createPrefix;
9
- function parseKey(prefixedKey) {
10
- const match = /^_([\d]+)_(.*)$/.exec(prefixedKey);
9
+ function matchKey(prefixedKey) {
10
+ const match = /^_(\d+)_(.*)$/.exec(prefixedKey);
11
11
  if (match && match.length === 3 && !isNaN(Number(match[1])) && match[2]) {
12
12
  return { index: Number(match[1]), originalKey: match[2] };
13
13
  }
14
- throw new Error(`Key ${prefixedKey} is not correctly prefixed`);
14
+ return null;
15
+ }
16
+ function parseKey(prefixedKey) {
17
+ const match = matchKey(prefixedKey);
18
+ if (!match) {
19
+ throw new Error(`Key ${prefixedKey} is not correctly prefixed`);
20
+ }
21
+ return match;
15
22
  }
16
23
  exports.parseKey = parseKey;
24
+ function parseKeyFromPath(path) {
25
+ let keyOffset = 0;
26
+ let match = null;
27
+ // Keep looping over path until we've found a match or path has no more items left
28
+ for (; !match && keyOffset < path.length; keyOffset++) {
29
+ const pathKey = path[keyOffset];
30
+ if (typeof pathKey === 'string') {
31
+ match = matchKey(pathKey);
32
+ }
33
+ }
34
+ if (!match) {
35
+ throw new Error(`Path ${path.join('.')} does not contain correctly prefixed key`);
36
+ }
37
+ return {
38
+ ...match,
39
+ keyOffset,
40
+ };
41
+ }
42
+ exports.parseKeyFromPath = parseKeyFromPath;
@@ -30,9 +30,8 @@ function splitResult({ data, errors }, numResults) {
30
30
  if (errors) {
31
31
  for (const error of errors) {
32
32
  if (error.path) {
33
- const parsedKey = (0, prefix_js_1.parseKey)(error.path[0]);
34
- const { index, originalKey } = parsedKey;
35
- const newError = (0, utils_1.relocatedError)(error, [originalKey, ...error.path.slice(1)]);
33
+ const { index, originalKey, keyOffset } = (0, prefix_js_1.parseKeyFromPath)(error.path);
34
+ const newError = (0, utils_1.relocatedError)(error, [originalKey, ...error.path.slice(keyOffset)]);
36
35
  const resultErrors = (splitResults[index].errors = (splitResults[index].errors ||
37
36
  []));
38
37
  resultErrors.push(newError);
package/esm/prefix.js CHANGED
@@ -2,10 +2,35 @@
2
2
  export function createPrefix(index) {
3
3
  return `_${index}_`;
4
4
  }
5
- export function parseKey(prefixedKey) {
6
- const match = /^_([\d]+)_(.*)$/.exec(prefixedKey);
5
+ function matchKey(prefixedKey) {
6
+ const match = /^_(\d+)_(.*)$/.exec(prefixedKey);
7
7
  if (match && match.length === 3 && !isNaN(Number(match[1])) && match[2]) {
8
8
  return { index: Number(match[1]), originalKey: match[2] };
9
9
  }
10
- throw new Error(`Key ${prefixedKey} is not correctly prefixed`);
10
+ return null;
11
+ }
12
+ export function parseKey(prefixedKey) {
13
+ const match = matchKey(prefixedKey);
14
+ if (!match) {
15
+ throw new Error(`Key ${prefixedKey} is not correctly prefixed`);
16
+ }
17
+ return match;
18
+ }
19
+ export function parseKeyFromPath(path) {
20
+ let keyOffset = 0;
21
+ let match = null;
22
+ // Keep looping over path until we've found a match or path has no more items left
23
+ for (; !match && keyOffset < path.length; keyOffset++) {
24
+ const pathKey = path[keyOffset];
25
+ if (typeof pathKey === 'string') {
26
+ match = matchKey(pathKey);
27
+ }
28
+ }
29
+ if (!match) {
30
+ throw new Error(`Path ${path.join('.')} does not contain correctly prefixed key`);
31
+ }
32
+ return {
33
+ ...match,
34
+ keyOffset,
35
+ };
11
36
  }
@@ -1,6 +1,6 @@
1
1
  // adapted from https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
2
2
  import { relocatedError } from '@graphql-tools/utils';
3
- import { parseKey } from './prefix.js';
3
+ import { parseKey, parseKeyFromPath } from './prefix.js';
4
4
  /**
5
5
  * Split and transform result of the query produced by the `merge` function
6
6
  */
@@ -27,9 +27,8 @@ export function splitResult({ data, errors }, numResults) {
27
27
  if (errors) {
28
28
  for (const error of errors) {
29
29
  if (error.path) {
30
- const parsedKey = parseKey(error.path[0]);
31
- const { index, originalKey } = parsedKey;
32
- const newError = relocatedError(error, [originalKey, ...error.path.slice(1)]);
30
+ const { index, originalKey, keyOffset } = parseKeyFromPath(error.path);
31
+ const newError = relocatedError(error, [originalKey, ...error.path.slice(keyOffset)]);
33
32
  const resultErrors = (splitResults[index].errors = (splitResults[index].errors ||
34
33
  []));
35
34
  resultErrors.push(newError);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/batch-execute",
3
- "version": "9.0.1",
3
+ "version": "9.0.2-rc-20230823115638-58a6e1fa",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -1,5 +1,9 @@
1
1
  export declare function createPrefix(index: string): string;
2
- export declare function parseKey(prefixedKey: string): {
2
+ export type KeyMatch = {
3
3
  index: number;
4
4
  originalKey: string;
5
5
  };
6
+ export declare function parseKey(prefixedKey: string): KeyMatch;
7
+ export declare function parseKeyFromPath(path: readonly (string | number)[]): KeyMatch & {
8
+ keyOffset: number;
9
+ };
@@ -1,5 +1,9 @@
1
1
  export declare function createPrefix(index: string): string;
2
- export declare function parseKey(prefixedKey: string): {
2
+ export type KeyMatch = {
3
3
  index: number;
4
4
  originalKey: string;
5
5
  };
6
+ export declare function parseKey(prefixedKey: string): KeyMatch;
7
+ export declare function parseKeyFromPath(path: readonly (string | number)[]): KeyMatch & {
8
+ keyOffset: number;
9
+ };