ValidationContext.js 6.8 KB

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