updateArgument.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createVariableNameGenerator = exports.updateArgument = void 0;
  4. const graphql_1 = require("graphql");
  5. const astFromType_js_1 = require("./astFromType.js");
  6. function 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. }
  43. exports.updateArgument = updateArgument;
  44. function 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. }
  54. exports.createVariableNameGenerator = createVariableNameGenerator;