UNPKG

1.81 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createVariableNameGenerator = exports.updateArgument = void 0;
4const graphql_1 = require("graphql");
5const astFromType_js_1 = require("./astFromType.js");
6function updateArgument(argumentNodes, variableDefinitionsMap, variableValues, argName, varName, type, value) {
7 argumentNodes[argName] = {
8 kind: graphql_1.Kind.ARGUMENT,
9 name: {
10 kind: graphql_1.Kind.NAME,
11 value: argName,
12 },
13 value: {
14 kind: graphql_1.Kind.VARIABLE,
15 name: {
16 kind: graphql_1.Kind.NAME,
17 value: varName,
18 },
19 },
20 };
21 variableDefinitionsMap[varName] = {
22 kind: graphql_1.Kind.VARIABLE_DEFINITION,
23 variable: {
24 kind: graphql_1.Kind.VARIABLE,
25 name: {
26 kind: graphql_1.Kind.NAME,
27 value: varName,
28 },
29 },
30 type: (0, astFromType_js_1.astFromType)(type),
31 };
32 if (value !== undefined) {
33 variableValues[varName] = value;
34 return;
35 }
36 // including the variable in the map with value of `undefined`
37 // will actually be translated by graphql-js into `null`
38 // see https://github.com/graphql/graphql-js/issues/2533
39 if (varName in variableValues) {
40 delete variableValues[varName];
41 }
42}
43exports.updateArgument = updateArgument;
44function createVariableNameGenerator(variableDefinitionMap) {
45 let varCounter = 0;
46 return (argName) => {
47 let varName;
48 do {
49 varName = `_v${(varCounter++).toString()}_${argName}`;
50 } while (varName in variableDefinitionMap);
51 return varName;
52 };
53}
54exports.createVariableNameGenerator = createVariableNameGenerator;