ValidationContext.mjs 6.5 KB

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