UNPKG

2.5 kBTypeScriptView Raw
1import Maybe from "../tsutils/Maybe";
2import { GraphQLError } from "../error";
3import {
4 DocumentNode,
5 OperationDefinitionNode,
6 VariableNode,
7 SelectionSetNode,
8 FragmentSpreadNode,
9 FragmentDefinitionNode,
10} from "../language/ast";
11import { GraphQLSchema } from "../type/schema";
12import {
13 GraphQLInputType,
14 GraphQLOutputType,
15 GraphQLCompositeType,
16 GraphQLField,
17 GraphQLArgument,
18} from "../type/definition";
19import { GraphQLDirective } from "../type/directives";
20import { TypeInfo } from "../utilities/TypeInfo";
21import { ASTVisitor } from "../language/visitor";
22
23type NodeWithSelectionSet = OperationDefinitionNode | FragmentDefinitionNode;
24type VariableUsage = {
25 readonly node: VariableNode;
26 readonly type: Maybe<GraphQLInputType>;
27 readonly defaultValue: Maybe<any>;
28};
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 */
35export class ASTValidationContext {
36 constructor(ast: DocumentNode);
37
38 reportError(error: GraphQLError): undefined;
39
40 getErrors(): ReadonlyArray<GraphQLError>;
41
42 getDocument(): DocumentNode;
43}
44
45export class SDLValidationContext extends ASTValidationContext {
46 constructor(ast: DocumentNode, schema?: Maybe<GraphQLSchema>);
47
48 getSchema(): Maybe<GraphQLSchema>;
49}
50
51export type SDLValidationRule = (context: SDLValidationContext) => ASTVisitor;
52
53export class ValidationContext extends ASTValidationContext {
54 constructor(schema: GraphQLSchema, ast: DocumentNode, typeInfo: TypeInfo);
55
56 getSchema(): GraphQLSchema;
57
58 getFragment(name: string): Maybe<FragmentDefinitionNode>;
59
60 getFragmentSpreads(node: SelectionSetNode): ReadonlyArray<FragmentSpreadNode>;
61
62 getRecursivelyReferencedFragments(operation: OperationDefinitionNode): ReadonlyArray<FragmentDefinitionNode>;
63
64 getVariableUsages(node: NodeWithSelectionSet): ReadonlyArray<VariableUsage>;
65
66 getRecursiveVariableUsages(operation: OperationDefinitionNode): ReadonlyArray<VariableUsage>;
67
68 getType(): Maybe<GraphQLOutputType>;
69
70 getParentType(): Maybe<GraphQLCompositeType>;
71
72 getInputType(): Maybe<GraphQLInputType>;
73
74 getParentInputType(): Maybe<GraphQLInputType>;
75
76 getFieldDef(): Maybe<GraphQLField<any, any>>;
77
78 getDirective(): Maybe<GraphQLDirective>;
79
80 getArgument(): Maybe<GraphQLArgument>;
81}
82
83export type ValidationRule = (context: ValidationContext) => ASTVisitor;