UNPKG

1.91 kBJavaScriptView Raw
1"use strict";
2// adapted from https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.splitResult = void 0;
5const utils_1 = require("@graphql-tools/utils");
6const prefix_js_1 = require("./prefix.js");
7/**
8 * Split and transform result of the query produced by the `merge` function
9 */
10function splitResult({ data, errors }, numResults) {
11 const splitResults = [];
12 for (let i = 0; i < numResults; i++) {
13 splitResults.push({});
14 }
15 if (data) {
16 for (const prefixedKey in data) {
17 const { index, originalKey } = (0, prefix_js_1.parseKey)(prefixedKey);
18 const result = splitResults[index];
19 if (result == null) {
20 continue;
21 }
22 if (result.data == null) {
23 result.data = { [originalKey]: data[prefixedKey] };
24 }
25 else {
26 result.data[originalKey] = data[prefixedKey];
27 }
28 }
29 }
30 if (errors) {
31 for (const error of errors) {
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)]);
36 const resultErrors = (splitResults[index].errors = (splitResults[index].errors || []));
37 resultErrors.push(newError);
38 }
39 else {
40 splitResults.forEach(result => {
41 const resultErrors = (result.errors = (result.errors || []));
42 resultErrors.push((0, utils_1.createGraphQLError)(error.message));
43 });
44 }
45 }
46 }
47 return splitResults;
48}
49exports.splitResult = splitResult;