AddArgumentsAsVariables.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __spreadArrays = (this && this.__spreadArrays) || function () {
  13. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  14. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  15. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  16. r[k] = a[j];
  17. return r;
  18. };
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. var graphql_1 = require("graphql");
  21. var AddArgumentsAsVariablesTransform = /** @class */ (function () {
  22. function AddArgumentsAsVariablesTransform(schema, args) {
  23. this.schema = schema;
  24. this.args = args;
  25. }
  26. AddArgumentsAsVariablesTransform.prototype.transformRequest = function (originalRequest) {
  27. var _a = addVariablesToRootField(this.schema, originalRequest.document, this.args), document = _a.document, newVariables = _a.newVariables;
  28. var variables = __assign(__assign({}, originalRequest.variables), newVariables);
  29. return {
  30. document: document,
  31. variables: variables,
  32. };
  33. };
  34. return AddArgumentsAsVariablesTransform;
  35. }());
  36. exports.default = AddArgumentsAsVariablesTransform;
  37. function addVariablesToRootField(targetSchema, document, args) {
  38. var operations = document.definitions.filter(function (def) { return def.kind === graphql_1.Kind.OPERATION_DEFINITION; });
  39. var fragments = document.definitions.filter(function (def) { return def.kind === graphql_1.Kind.FRAGMENT_DEFINITION; });
  40. var variableNames = {};
  41. var newOperations = operations.map(function (operation) {
  42. var existingVariables = operation.variableDefinitions.map(function (variableDefinition) {
  43. return variableDefinition.variable.name.value;
  44. });
  45. var variableCounter = 0;
  46. var variables = {};
  47. var generateVariableName = function (argName) {
  48. var varName;
  49. do {
  50. varName = "_v" + variableCounter + "_" + argName;
  51. variableCounter++;
  52. } while (existingVariables.indexOf(varName) !== -1);
  53. return varName;
  54. };
  55. var type;
  56. if (operation.operation === 'subscription') {
  57. type = targetSchema.getSubscriptionType();
  58. }
  59. else if (operation.operation === 'mutation') {
  60. type = targetSchema.getMutationType();
  61. }
  62. else {
  63. type = targetSchema.getQueryType();
  64. }
  65. var newSelectionSet = [];
  66. operation.selectionSet.selections.forEach(function (selection) {
  67. if (selection.kind === graphql_1.Kind.FIELD) {
  68. var newArgs_1 = {};
  69. selection.arguments.forEach(function (argument) {
  70. newArgs_1[argument.name.value] = argument;
  71. });
  72. var name_1 = selection.name.value;
  73. var field = type.getFields()[name_1];
  74. field.args.forEach(function (argument) {
  75. if (argument.name in args) {
  76. var variableName = generateVariableName(argument.name);
  77. variableNames[argument.name] = variableName;
  78. newArgs_1[argument.name] = {
  79. kind: graphql_1.Kind.ARGUMENT,
  80. name: {
  81. kind: graphql_1.Kind.NAME,
  82. value: argument.name,
  83. },
  84. value: {
  85. kind: graphql_1.Kind.VARIABLE,
  86. name: {
  87. kind: graphql_1.Kind.NAME,
  88. value: variableName,
  89. },
  90. },
  91. };
  92. existingVariables.push(variableName);
  93. variables[variableName] = {
  94. kind: graphql_1.Kind.VARIABLE_DEFINITION,
  95. variable: {
  96. kind: graphql_1.Kind.VARIABLE,
  97. name: {
  98. kind: graphql_1.Kind.NAME,
  99. value: variableName,
  100. },
  101. },
  102. type: typeToAst(argument.type),
  103. };
  104. }
  105. });
  106. newSelectionSet.push(__assign(__assign({}, selection), { arguments: Object.keys(newArgs_1).map(function (argName) { return newArgs_1[argName]; }) }));
  107. }
  108. else {
  109. newSelectionSet.push(selection);
  110. }
  111. });
  112. return __assign(__assign({}, operation), { variableDefinitions: operation.variableDefinitions.concat(Object.keys(variables).map(function (varName) { return variables[varName]; })), selectionSet: {
  113. kind: graphql_1.Kind.SELECTION_SET,
  114. selections: newSelectionSet,
  115. } });
  116. });
  117. var newVariables = {};
  118. Object.keys(variableNames).forEach(function (name) {
  119. newVariables[variableNames[name]] = args[name];
  120. });
  121. return {
  122. document: __assign(__assign({}, document), { definitions: __spreadArrays(newOperations, fragments) }),
  123. newVariables: newVariables,
  124. };
  125. }
  126. function typeToAst(type) {
  127. if (type instanceof graphql_1.GraphQLNonNull) {
  128. var innerType = typeToAst(type.ofType);
  129. if (innerType.kind === graphql_1.Kind.LIST_TYPE ||
  130. innerType.kind === graphql_1.Kind.NAMED_TYPE) {
  131. return {
  132. kind: graphql_1.Kind.NON_NULL_TYPE,
  133. type: innerType,
  134. };
  135. }
  136. else {
  137. throw new Error('Incorrent inner non-null type');
  138. }
  139. }
  140. else if (type instanceof graphql_1.GraphQLList) {
  141. return {
  142. kind: graphql_1.Kind.LIST_TYPE,
  143. type: typeToAst(type.ofType),
  144. };
  145. }
  146. else {
  147. return {
  148. kind: graphql_1.Kind.NAMED_TYPE,
  149. name: {
  150. kind: graphql_1.Kind.NAME,
  151. value: type.toString(),
  152. },
  153. };
  154. }
  155. }
  156. //# sourceMappingURL=AddArgumentsAsVariables.js.map