ValidationContext.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ValidationContext = exports.SDLValidationContext = exports.ASTValidationContext = void 0;
  6. var _kinds = require("../language/kinds.js");
  7. var _visitor = require("../language/visitor.js");
  8. var _TypeInfo = require("../utilities/TypeInfo.js");
  9. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  10. /**
  11. * An instance of this class is passed as the "this" context to all validators,
  12. * allowing access to commonly useful contextual information from within a
  13. * validation rule.
  14. */
  15. var ASTValidationContext = /*#__PURE__*/function () {
  16. function ASTValidationContext(ast, onError) {
  17. this._ast = ast;
  18. this._fragments = undefined;
  19. this._fragmentSpreads = new Map();
  20. this._recursivelyReferencedFragments = new Map();
  21. this._onError = onError;
  22. }
  23. var _proto = ASTValidationContext.prototype;
  24. _proto.reportError = function reportError(error) {
  25. this._onError(error);
  26. };
  27. _proto.getDocument = function getDocument() {
  28. return this._ast;
  29. };
  30. _proto.getFragment = function getFragment(name) {
  31. var fragments = this._fragments;
  32. if (!fragments) {
  33. this._fragments = fragments = this.getDocument().definitions.reduce(function (frags, statement) {
  34. if (statement.kind === _kinds.Kind.FRAGMENT_DEFINITION) {
  35. frags[statement.name.value] = statement;
  36. }
  37. return frags;
  38. }, Object.create(null));
  39. }
  40. return fragments[name];
  41. };
  42. _proto.getFragmentSpreads = function getFragmentSpreads(node) {
  43. var spreads = this._fragmentSpreads.get(node);
  44. if (!spreads) {
  45. spreads = [];
  46. var setsToVisit = [node];
  47. while (setsToVisit.length !== 0) {
  48. var set = setsToVisit.pop();
  49. for (var _i2 = 0, _set$selections2 = set.selections; _i2 < _set$selections2.length; _i2++) {
  50. var selection = _set$selections2[_i2];
  51. if (selection.kind === _kinds.Kind.FRAGMENT_SPREAD) {
  52. spreads.push(selection);
  53. } else if (selection.selectionSet) {
  54. setsToVisit.push(selection.selectionSet);
  55. }
  56. }
  57. }
  58. this._fragmentSpreads.set(node, spreads);
  59. }
  60. return spreads;
  61. };
  62. _proto.getRecursivelyReferencedFragments = function getRecursivelyReferencedFragments(operation) {
  63. var fragments = this._recursivelyReferencedFragments.get(operation);
  64. if (!fragments) {
  65. fragments = [];
  66. var collectedNames = Object.create(null);
  67. var nodesToVisit = [operation.selectionSet];
  68. while (nodesToVisit.length !== 0) {
  69. var node = nodesToVisit.pop();
  70. for (var _i4 = 0, _this$getFragmentSpre2 = this.getFragmentSpreads(node); _i4 < _this$getFragmentSpre2.length; _i4++) {
  71. var spread = _this$getFragmentSpre2[_i4];
  72. var fragName = spread.name.value;
  73. if (collectedNames[fragName] !== true) {
  74. collectedNames[fragName] = true;
  75. var fragment = this.getFragment(fragName);
  76. if (fragment) {
  77. fragments.push(fragment);
  78. nodesToVisit.push(fragment.selectionSet);
  79. }
  80. }
  81. }
  82. }
  83. this._recursivelyReferencedFragments.set(operation, fragments);
  84. }
  85. return fragments;
  86. };
  87. return ASTValidationContext;
  88. }();
  89. exports.ASTValidationContext = ASTValidationContext;
  90. var SDLValidationContext = /*#__PURE__*/function (_ASTValidationContext) {
  91. _inheritsLoose(SDLValidationContext, _ASTValidationContext);
  92. function SDLValidationContext(ast, schema, onError) {
  93. var _this;
  94. _this = _ASTValidationContext.call(this, ast, onError) || this;
  95. _this._schema = schema;
  96. return _this;
  97. }
  98. var _proto2 = SDLValidationContext.prototype;
  99. _proto2.getSchema = function getSchema() {
  100. return this._schema;
  101. };
  102. return SDLValidationContext;
  103. }(ASTValidationContext);
  104. exports.SDLValidationContext = SDLValidationContext;
  105. var ValidationContext = /*#__PURE__*/function (_ASTValidationContext2) {
  106. _inheritsLoose(ValidationContext, _ASTValidationContext2);
  107. function ValidationContext(schema, ast, typeInfo, onError) {
  108. var _this2;
  109. _this2 = _ASTValidationContext2.call(this, ast, onError) || this;
  110. _this2._schema = schema;
  111. _this2._typeInfo = typeInfo;
  112. _this2._variableUsages = new Map();
  113. _this2._recursiveVariableUsages = new Map();
  114. return _this2;
  115. }
  116. var _proto3 = ValidationContext.prototype;
  117. _proto3.getSchema = function getSchema() {
  118. return this._schema;
  119. };
  120. _proto3.getVariableUsages = function getVariableUsages(node) {
  121. var usages = this._variableUsages.get(node);
  122. if (!usages) {
  123. var newUsages = [];
  124. var typeInfo = new _TypeInfo.TypeInfo(this._schema);
  125. (0, _visitor.visit)(node, (0, _TypeInfo.visitWithTypeInfo)(typeInfo, {
  126. VariableDefinition: function VariableDefinition() {
  127. return false;
  128. },
  129. Variable: function Variable(variable) {
  130. newUsages.push({
  131. node: variable,
  132. type: typeInfo.getInputType(),
  133. defaultValue: typeInfo.getDefaultValue()
  134. });
  135. }
  136. }));
  137. usages = newUsages;
  138. this._variableUsages.set(node, usages);
  139. }
  140. return usages;
  141. };
  142. _proto3.getRecursiveVariableUsages = function getRecursiveVariableUsages(operation) {
  143. var usages = this._recursiveVariableUsages.get(operation);
  144. if (!usages) {
  145. usages = this.getVariableUsages(operation);
  146. for (var _i6 = 0, _this$getRecursivelyR2 = this.getRecursivelyReferencedFragments(operation); _i6 < _this$getRecursivelyR2.length; _i6++) {
  147. var frag = _this$getRecursivelyR2[_i6];
  148. usages = usages.concat(this.getVariableUsages(frag));
  149. }
  150. this._recursiveVariableUsages.set(operation, usages);
  151. }
  152. return usages;
  153. };
  154. _proto3.getType = function getType() {
  155. return this._typeInfo.getType();
  156. };
  157. _proto3.getParentType = function getParentType() {
  158. return this._typeInfo.getParentType();
  159. };
  160. _proto3.getInputType = function getInputType() {
  161. return this._typeInfo.getInputType();
  162. };
  163. _proto3.getParentInputType = function getParentInputType() {
  164. return this._typeInfo.getParentInputType();
  165. };
  166. _proto3.getFieldDef = function getFieldDef() {
  167. return this._typeInfo.getFieldDef();
  168. };
  169. _proto3.getDirective = function getDirective() {
  170. return this._typeInfo.getDirective();
  171. };
  172. _proto3.getArgument = function getArgument() {
  173. return this._typeInfo.getArgument();
  174. };
  175. _proto3.getEnumValue = function getEnumValue() {
  176. return this._typeInfo.getEnumValue();
  177. };
  178. return ValidationContext;
  179. }(ASTValidationContext);
  180. exports.ValidationContext = ValidationContext;