UNPKG

1.96 kBTypeScriptView Raw
1import { Maybe } from '../jsutils/Maybe';
2
3import { Visitor } from '../language/visitor';
4import { ASTNode, ASTKindToNode, FieldNode } from '../language/ast';
5import { GraphQLSchema } from '../type/schema';
6import { GraphQLDirective } from '../type/directives';
7import {
8 GraphQLType,
9 GraphQLInputType,
10 GraphQLOutputType,
11 GraphQLCompositeType,
12 GraphQLField,
13 GraphQLArgument,
14 GraphQLEnumValue,
15} from '../type/definition';
16
17/**
18 * TypeInfo is a utility class which, given a GraphQL schema, can keep track
19 * of the current field and type definitions at any point in a GraphQL document
20 * AST during a recursive descent by calling `enter(node)` and `leave(node)`.
21 */
22export class TypeInfo {
23 constructor(
24 schema: GraphQLSchema,
25 // NOTE: this experimental optional second parameter is only needed in order
26 // to support non-spec-compliant code bases. You should never need to use it.
27 // It may disappear in the future.
28 getFieldDefFn?: getFieldDef,
29 // Initial type may be provided in rare cases to facilitate traversals
30 // beginning somewhere other than documents.
31 initialType?: GraphQLType,
32 );
33
34 getType(): Maybe<GraphQLOutputType>;
35 getParentType(): Maybe<GraphQLCompositeType>;
36 getInputType(): Maybe<GraphQLInputType>;
37 getParentInputType(): Maybe<GraphQLInputType>;
38 getFieldDef(): GraphQLField<any, Maybe<any>>;
39 getDefaultValue(): Maybe<any>;
40 getDirective(): Maybe<GraphQLDirective>;
41 getArgument(): Maybe<GraphQLArgument>;
42 getEnumValue(): Maybe<GraphQLEnumValue>;
43 enter(node: ASTNode): any;
44 leave(node: ASTNode): any;
45}
46
47type getFieldDef = (
48 schema: GraphQLSchema,
49 parentType: GraphQLType,
50 fieldNode: FieldNode,
51) => Maybe<GraphQLField<any, any>>;
52
53/**
54 * Creates a new visitor instance which maintains a provided TypeInfo instance
55 * along with visiting visitor.
56 */
57export function visitWithTypeInfo(
58 typeInfo: TypeInfo,
59 visitor: Visitor<ASTKindToNode>,
60): Visitor<ASTKindToNode>;