ValidationContext.js 5.1 KB

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