ValidationContext.mjs 6.4 KB

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