ValidationContext.d.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { Maybe } from '../jsutils/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. GraphQLEnumValue,
  21. } from '../type/definition';
  22. import { TypeInfo } from '../utilities/TypeInfo';
  23. type NodeWithSelectionSet = OperationDefinitionNode | FragmentDefinitionNode;
  24. interface VariableUsage {
  25. readonly node: VariableNode;
  26. readonly type: Maybe<GraphQLInputType>;
  27. readonly defaultValue: Maybe<any>;
  28. }
  29. /**
  30. * An instance of this class is passed as the "this" context to all validators,
  31. * allowing access to commonly useful contextual information from within a
  32. * validation rule.
  33. */
  34. export class ASTValidationContext {
  35. constructor(ast: DocumentNode, onError: (err: GraphQLError) => void);
  36. reportError(error: GraphQLError): undefined;
  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. getEnumValue(): Maybe<GraphQLEnumValue>;
  73. }
  74. export type ValidationRule = (context: ValidationContext) => ASTVisitor;