ValidationContext.js.flow 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // @flow strict
  2. import { type ObjMap } from '../jsutils/ObjMap';
  3. import { type GraphQLError } from '../error/GraphQLError';
  4. import { Kind } from '../language/kinds';
  5. import { type ASTVisitor, visit, visitWithTypeInfo } from '../language/visitor';
  6. import {
  7. type DocumentNode,
  8. type OperationDefinitionNode,
  9. type VariableNode,
  10. type SelectionSetNode,
  11. type FragmentSpreadNode,
  12. type FragmentDefinitionNode,
  13. } from '../language/ast';
  14. import { type GraphQLSchema } from '../type/schema';
  15. import { type GraphQLDirective } from '../type/directives';
  16. import {
  17. type GraphQLInputType,
  18. type GraphQLOutputType,
  19. type GraphQLCompositeType,
  20. type GraphQLField,
  21. type GraphQLArgument,
  22. } from '../type/definition';
  23. import { TypeInfo } from '../utilities/TypeInfo';
  24. type NodeWithSelectionSet = OperationDefinitionNode | FragmentDefinitionNode;
  25. type VariableUsage = {|
  26. +node: VariableNode,
  27. +type: ?GraphQLInputType,
  28. +defaultValue: ?mixed,
  29. |};
  30. /**
  31. * An instance of this class is passed as the "this" context to all validators,
  32. * allowing access to commonly useful contextual information from within a
  33. * validation rule.
  34. */
  35. export class ASTValidationContext {
  36. _ast: DocumentNode;
  37. _onError: ?(err: GraphQLError) => void;
  38. _errors: Array<GraphQLError>;
  39. _fragments: ?ObjMap<FragmentDefinitionNode>;
  40. _fragmentSpreads: Map<SelectionSetNode, $ReadOnlyArray<FragmentSpreadNode>>;
  41. _recursivelyReferencedFragments: Map<
  42. OperationDefinitionNode,
  43. $ReadOnlyArray<FragmentDefinitionNode>,
  44. >;
  45. constructor(ast: DocumentNode, onError?: (err: GraphQLError) => void): void {
  46. this._ast = ast;
  47. this._errors = [];
  48. this._fragments = undefined;
  49. this._fragmentSpreads = new Map();
  50. this._recursivelyReferencedFragments = new Map();
  51. this._onError = onError;
  52. }
  53. reportError(error: GraphQLError): void {
  54. this._errors.push(error);
  55. if (this._onError) {
  56. this._onError(error);
  57. }
  58. }
  59. // @deprecated: use onError callback instead - will be removed in v15.
  60. getErrors(): $ReadOnlyArray<GraphQLError> {
  61. return this._errors;
  62. }
  63. getDocument(): DocumentNode {
  64. return this._ast;
  65. }
  66. getFragment(name: string): ?FragmentDefinitionNode {
  67. let fragments = this._fragments;
  68. if (!fragments) {
  69. this._fragments = fragments = this.getDocument().definitions.reduce(
  70. (frags, statement) => {
  71. if (statement.kind === Kind.FRAGMENT_DEFINITION) {
  72. frags[statement.name.value] = statement;
  73. }
  74. return frags;
  75. },
  76. Object.create(null),
  77. );
  78. }
  79. return fragments[name];
  80. }
  81. getFragmentSpreads(
  82. node: SelectionSetNode,
  83. ): $ReadOnlyArray<FragmentSpreadNode> {
  84. let spreads = this._fragmentSpreads.get(node);
  85. if (!spreads) {
  86. spreads = [];
  87. const setsToVisit: Array<SelectionSetNode> = [node];
  88. while (setsToVisit.length !== 0) {
  89. const set = setsToVisit.pop();
  90. for (const selection of set.selections) {
  91. if (selection.kind === Kind.FRAGMENT_SPREAD) {
  92. spreads.push(selection);
  93. } else if (selection.selectionSet) {
  94. setsToVisit.push(selection.selectionSet);
  95. }
  96. }
  97. }
  98. this._fragmentSpreads.set(node, spreads);
  99. }
  100. return spreads;
  101. }
  102. getRecursivelyReferencedFragments(
  103. operation: OperationDefinitionNode,
  104. ): $ReadOnlyArray<FragmentDefinitionNode> {
  105. let fragments = this._recursivelyReferencedFragments.get(operation);
  106. if (!fragments) {
  107. fragments = [];
  108. const collectedNames = Object.create(null);
  109. const nodesToVisit: Array<SelectionSetNode> = [operation.selectionSet];
  110. while (nodesToVisit.length !== 0) {
  111. const node = nodesToVisit.pop();
  112. for (const spread of this.getFragmentSpreads(node)) {
  113. const fragName = spread.name.value;
  114. if (collectedNames[fragName] !== true) {
  115. collectedNames[fragName] = true;
  116. const fragment = this.getFragment(fragName);
  117. if (fragment) {
  118. fragments.push(fragment);
  119. nodesToVisit.push(fragment.selectionSet);
  120. }
  121. }
  122. }
  123. }
  124. this._recursivelyReferencedFragments.set(operation, fragments);
  125. }
  126. return fragments;
  127. }
  128. }
  129. export type ASTValidationRule = ASTValidationContext => ASTVisitor;
  130. export class SDLValidationContext extends ASTValidationContext {
  131. _schema: ?GraphQLSchema;
  132. constructor(
  133. ast: DocumentNode,
  134. schema: ?GraphQLSchema,
  135. onError?: (err: GraphQLError) => void,
  136. ): void {
  137. super(ast, onError);
  138. this._schema = schema;
  139. }
  140. getSchema(): ?GraphQLSchema {
  141. return this._schema;
  142. }
  143. }
  144. export type SDLValidationRule = SDLValidationContext => ASTVisitor;
  145. export class ValidationContext extends ASTValidationContext {
  146. _schema: GraphQLSchema;
  147. _typeInfo: TypeInfo;
  148. _variableUsages: Map<NodeWithSelectionSet, $ReadOnlyArray<VariableUsage>>;
  149. _recursiveVariableUsages: Map<
  150. OperationDefinitionNode,
  151. $ReadOnlyArray<VariableUsage>,
  152. >;
  153. constructor(
  154. schema: GraphQLSchema,
  155. ast: DocumentNode,
  156. typeInfo: TypeInfo,
  157. onError?: (err: GraphQLError) => void,
  158. ): void {
  159. super(ast, onError);
  160. this._schema = schema;
  161. this._typeInfo = typeInfo;
  162. this._variableUsages = new Map();
  163. this._recursiveVariableUsages = new Map();
  164. }
  165. getSchema(): GraphQLSchema {
  166. return this._schema;
  167. }
  168. getVariableUsages(node: NodeWithSelectionSet): $ReadOnlyArray<VariableUsage> {
  169. let usages = this._variableUsages.get(node);
  170. if (!usages) {
  171. const newUsages = [];
  172. const typeInfo = new TypeInfo(this._schema);
  173. visit(
  174. node,
  175. visitWithTypeInfo(typeInfo, {
  176. VariableDefinition: () => false,
  177. Variable(variable) {
  178. newUsages.push({
  179. node: variable,
  180. type: typeInfo.getInputType(),
  181. defaultValue: typeInfo.getDefaultValue(),
  182. });
  183. },
  184. }),
  185. );
  186. usages = newUsages;
  187. this._variableUsages.set(node, usages);
  188. }
  189. return usages;
  190. }
  191. getRecursiveVariableUsages(
  192. operation: OperationDefinitionNode,
  193. ): $ReadOnlyArray<VariableUsage> {
  194. let usages = this._recursiveVariableUsages.get(operation);
  195. if (!usages) {
  196. usages = this.getVariableUsages(operation);
  197. for (const frag of this.getRecursivelyReferencedFragments(operation)) {
  198. usages = usages.concat(this.getVariableUsages(frag));
  199. }
  200. this._recursiveVariableUsages.set(operation, usages);
  201. }
  202. return usages;
  203. }
  204. getType(): ?GraphQLOutputType {
  205. return this._typeInfo.getType();
  206. }
  207. getParentType(): ?GraphQLCompositeType {
  208. return this._typeInfo.getParentType();
  209. }
  210. getInputType(): ?GraphQLInputType {
  211. return this._typeInfo.getInputType();
  212. }
  213. getParentInputType(): ?GraphQLInputType {
  214. return this._typeInfo.getParentInputType();
  215. }
  216. getFieldDef(): ?GraphQLField<mixed, mixed> {
  217. return this._typeInfo.getFieldDef();
  218. }
  219. getDirective(): ?GraphQLDirective {
  220. return this._typeInfo.getDirective();
  221. }
  222. getArgument(): ?GraphQLArgument {
  223. return this._typeInfo.getArgument();
  224. }
  225. }
  226. export type ValidationRule = ValidationContext => ASTVisitor;