UNPKG

2.59 kBTypeScriptView Raw
1import Maybe from '../tsutils/Maybe';
2import { GraphQLError } from '../error/GraphQLError';
3import { ASTVisitor } from '../language/visitor';
4import {
5 DocumentNode,
6 OperationDefinitionNode,
7 VariableNode,
8 SelectionSetNode,
9 FragmentSpreadNode,
10 FragmentDefinitionNode,
11} from '../language/ast';
12import { GraphQLSchema } from '../type/schema';
13import { GraphQLDirective } from '../type/directives';
14import {
15 GraphQLInputType,
16 GraphQLOutputType,
17 GraphQLCompositeType,
18 GraphQLField,
19 GraphQLArgument,
20} from '../type/definition';
21import { TypeInfo } from '../utilities/TypeInfo';
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 getFragment(name: string): Maybe<FragmentDefinitionNode>;
45
46 getFragmentSpreads(node: SelectionSetNode): ReadonlyArray<FragmentSpreadNode>;
47
48 getRecursivelyReferencedFragments(
49 operation: OperationDefinitionNode,
50 ): ReadonlyArray<FragmentDefinitionNode>;
51}
52
53export class SDLValidationContext extends ASTValidationContext {
54 constructor(
55 ast: DocumentNode,
56 schema: Maybe<GraphQLSchema>,
57 onError?: (err: GraphQLError) => void,
58 );
59
60 getSchema(): Maybe<GraphQLSchema>;
61}
62
63export type SDLValidationRule = (context: SDLValidationContext) => ASTVisitor;
64
65export class ValidationContext extends ASTValidationContext {
66 constructor(
67 schema: GraphQLSchema,
68 ast: DocumentNode,
69 typeInfo: TypeInfo,
70 onError?: (err: GraphQLError) => void,
71 );
72
73 getSchema(): GraphQLSchema;
74
75 getVariableUsages(node: NodeWithSelectionSet): ReadonlyArray<VariableUsage>;
76
77 getRecursivelyReferencedFragments(
78 operation: OperationDefinitionNode,
79 ): ReadonlyArray<FragmentDefinitionNode>;
80
81 getType(): Maybe<GraphQLOutputType>;
82
83 getParentType(): Maybe<GraphQLCompositeType>;
84
85 getInputType(): Maybe<GraphQLInputType>;
86
87 getParentInputType(): Maybe<GraphQLInputType>;
88
89 getFieldDef(): Maybe<GraphQLField<any, any>>;
90
91 getDirective(): Maybe<GraphQLDirective>;
92
93 getArgument(): Maybe<GraphQLArgument>;
94}
95
96export type ValidationRule = (context: ValidationContext) => ASTVisitor;