ValidationContext.d.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import Maybe from '../tsutils/Maybe';
  2. import { GraphQLError } from '../error/GraphQLError';
  3. import { ASTVisitor } from '../language/visitor';
  4. import {
  5. DocumentNode,
  6. OperationDefinitionNode,
  7. VariableNode,
  8. SelectionSetNode,
  9. FragmentSpreadNode,
  10. FragmentDefinitionNode,
  11. } from '../language/ast';
  12. import { GraphQLSchema } from '../type/schema';
  13. import { GraphQLDirective } from '../type/directives';
  14. import {
  15. GraphQLInputType,
  16. GraphQLOutputType,
  17. GraphQLCompositeType,
  18. GraphQLField,
  19. GraphQLArgument,
  20. } from '../type/definition';
  21. import { TypeInfo } from '../utilities/TypeInfo';
  22. type NodeWithSelectionSet = OperationDefinitionNode | FragmentDefinitionNode;
  23. type VariableUsage = {
  24. readonly node: VariableNode;
  25. readonly type: Maybe<GraphQLInputType>;
  26. readonly defaultValue: Maybe<any>;
  27. };
  28. /**
  29. * An instance of this class is passed as the "this" context to all validators,
  30. * allowing access to commonly useful contextual information from within a
  31. * validation rule.
  32. */
  33. export class ASTValidationContext {
  34. constructor(ast: DocumentNode);
  35. reportError(error: GraphQLError): undefined;
  36. getErrors(): ReadonlyArray<GraphQLError>;
  37. getDocument(): DocumentNode;
  38. getFragment(name: string): Maybe<FragmentDefinitionNode>;
  39. getFragmentSpreads(node: SelectionSetNode): ReadonlyArray<FragmentSpreadNode>;
  40. getRecursivelyReferencedFragments(
  41. operation: OperationDefinitionNode,
  42. ): ReadonlyArray<FragmentDefinitionNode>;
  43. }
  44. export class SDLValidationContext extends ASTValidationContext {
  45. constructor(
  46. ast: DocumentNode,
  47. schema: Maybe<GraphQLSchema>,
  48. onError?: (err: GraphQLError) => void,
  49. );
  50. getSchema(): Maybe<GraphQLSchema>;
  51. }
  52. export type SDLValidationRule = (context: SDLValidationContext) => ASTVisitor;
  53. export class ValidationContext extends ASTValidationContext {
  54. constructor(
  55. schema: GraphQLSchema,
  56. ast: DocumentNode,
  57. typeInfo: TypeInfo,
  58. onError?: (err: GraphQLError) => void,
  59. );
  60. getSchema(): GraphQLSchema;
  61. getVariableUsages(node: NodeWithSelectionSet): ReadonlyArray<VariableUsage>;
  62. getRecursivelyReferencedFragments(
  63. operation: OperationDefinitionNode,
  64. ): ReadonlyArray<FragmentDefinitionNode>;
  65. getType(): Maybe<GraphQLOutputType>;
  66. getParentType(): Maybe<GraphQLCompositeType>;
  67. getInputType(): Maybe<GraphQLInputType>;
  68. getParentInputType(): Maybe<GraphQLInputType>;
  69. getFieldDef(): Maybe<GraphQLField<any, any>>;
  70. getDirective(): Maybe<GraphQLDirective>;
  71. getArgument(): Maybe<GraphQLArgument>;
  72. }
  73. export type ValidationRule = (context: ValidationContext) => ASTVisitor;