1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseMergeArgsExpr = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const extractVariables_js_1 = require("./extractVariables.js");
|
6 | const preparseMergeArgsExpr_js_1 = require("./preparseMergeArgsExpr.js");
|
7 | const properties_js_1 = require("./properties.js");
|
8 | const getSourcePaths_js_1 = require("./getSourcePaths.js");
|
9 | function parseMergeArgsExpr(mergeArgsExpr, selectionSet) {
|
10 | const { mergeArgsExpr: newMergeArgsExpr, expansionExpressions } = (0, preparseMergeArgsExpr_js_1.preparseMergeArgsExpr)(mergeArgsExpr);
|
11 | const inputValue = (0, graphql_1.parseValue)(`{ ${newMergeArgsExpr} }`, { noLocation: true });
|
12 | const { inputValue: newInputValue, variablePaths } = (0, extractVariables_js_1.extractVariables)(inputValue);
|
13 | if (!Object.keys(expansionExpressions).length) {
|
14 | if (!Object.keys(variablePaths).length) {
|
15 | throw new Error('Merge arguments must declare a key.');
|
16 | }
|
17 | const mappingInstructions = getMappingInstructions(variablePaths);
|
18 | const usedProperties = (0, properties_js_1.propertyTreeFromPaths)((0, getSourcePaths_js_1.getSourcePaths)(mappingInstructions, selectionSet));
|
19 | return { args: (0, graphql_1.valueFromASTUntyped)(newInputValue), usedProperties, mappingInstructions };
|
20 | }
|
21 | const expansionRegEx = new RegExp(`^${preparseMergeArgsExpr_js_1.EXPANSION_PREFIX}[0-9]+$`);
|
22 | for (const variableName in variablePaths) {
|
23 | if (!variableName.match(expansionRegEx)) {
|
24 | throw new Error('Expansions cannot be mixed with single key declarations.');
|
25 | }
|
26 | }
|
27 | const expansions = [];
|
28 | const sourcePaths = [];
|
29 | for (const variableName in expansionExpressions) {
|
30 | const str = expansionExpressions[variableName];
|
31 | const valuePath = variablePaths[variableName];
|
32 | const { inputValue: expansionInputValue, variablePaths: expansionVariablePaths } = (0, extractVariables_js_1.extractVariables)((0, graphql_1.parseValue)(`${str}`, { noLocation: true }));
|
33 | if (!Object.keys(expansionVariablePaths).length) {
|
34 | throw new Error('Merge arguments must declare a key.');
|
35 | }
|
36 | const mappingInstructions = getMappingInstructions(expansionVariablePaths);
|
37 | const value = (0, graphql_1.valueFromASTUntyped)(expansionInputValue);
|
38 | sourcePaths.push(...(0, getSourcePaths_js_1.getSourcePaths)(mappingInstructions, selectionSet));
|
39 | assertNotWithinList(valuePath);
|
40 | expansions.push({
|
41 | valuePath,
|
42 | value,
|
43 | mappingInstructions,
|
44 | });
|
45 | }
|
46 | const usedProperties = (0, properties_js_1.propertyTreeFromPaths)(sourcePaths);
|
47 | return { args: (0, graphql_1.valueFromASTUntyped)(newInputValue), usedProperties, expansions };
|
48 | }
|
49 | exports.parseMergeArgsExpr = parseMergeArgsExpr;
|
50 | function getMappingInstructions(variablePaths) {
|
51 | const mappingInstructions = [];
|
52 | for (const keyPath in variablePaths) {
|
53 | const valuePath = variablePaths[keyPath];
|
54 | const splitKeyPath = keyPath.split(preparseMergeArgsExpr_js_1.KEY_DELIMITER).slice(1);
|
55 | assertNotWithinList(valuePath);
|
56 | mappingInstructions.push({
|
57 | destinationPath: valuePath,
|
58 | sourcePath: splitKeyPath,
|
59 | });
|
60 | }
|
61 | return mappingInstructions;
|
62 | }
|
63 | function assertNotWithinList(path) {
|
64 | for (const pathSegment of path) {
|
65 | if (typeof pathSegment === 'number') {
|
66 | throw new Error('Insertions cannot be made into a list.');
|
67 | }
|
68 | }
|
69 | }
|