@graphql-tools/utils
Version:
Common package containing utils and types for GraphQL tools
55 lines (54 loc) • 1.79 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateArgument = updateArgument;
exports.createVariableNameGenerator = createVariableNameGenerator;
const graphql_1 = require("graphql");
const astFromType_js_1 = require("./astFromType.js");
function updateArgument(argumentNodes, variableDefinitionsMap, variableValues, argName, varName, type, value) {
argumentNodes[argName] = {
kind: graphql_1.Kind.ARGUMENT,
name: {
kind: graphql_1.Kind.NAME,
value: argName,
},
value: {
kind: graphql_1.Kind.VARIABLE,
name: {
kind: graphql_1.Kind.NAME,
value: varName,
},
},
};
variableDefinitionsMap[varName] = {
kind: graphql_1.Kind.VARIABLE_DEFINITION,
variable: {
kind: graphql_1.Kind.VARIABLE,
name: {
kind: graphql_1.Kind.NAME,
value: varName,
},
},
type: (0, astFromType_js_1.astFromType)(type),
};
if (value !== undefined) {
variableValues[varName] = value;
return;
}
// including the variable in the map with value of `undefined`
// will actually be translated by graphql-js into `null`
// see https://github.com/graphql/graphql-js/issues/2533
if (varName in variableValues) {
delete variableValues[varName];
}
}
function createVariableNameGenerator(variableDefinitionMap) {
let varCounter = 0;
return (argName) => {
let varName;
do {
varName = varCounter === 0 ? argName : `_v${varCounter.toString()}_${argName}`;
varCounter++;
} while (varName in variableDefinitionMap);
return varName;
};
}
;