graphql/utilities/type-info

Classes

TypeInfo

TypeInfo is a utility class which, given a GraphQL schema, can keep track of the current field and type definitions at any point in a GraphQL document AST during a recursive descent by calling enter(node) and leave(node).

Constructor

Creates a TypeInfo instance.

Signature:

new TypeInfo(schema: GraphQLSchema, initialType?: null | undefined | GraphQLType, getFieldDefFn?: (schema: GraphQLSchema, parentType: GraphQLType, fieldNode: FieldNode) => null | undefined | GraphQLField<unknown, unknown>)
Arguments
NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.
initialType?null | undefined | GraphQLTypeInitial traversal type when traversal starts below a document.
getFieldDefFn?(schema: GraphQLSchema, parentType: GraphQLType, fieldNode: FieldNode) => null | undefined | GraphQLField<unknown, unknown>Optional field definition lookup override.
Returns
TypeDescription
TypeInfo

getType()

Returns the current output type at this point in traversal.

Signature:

getType(): null | undefined | GraphQLOutputType
Returns
TypeDescription
null | undefined | GraphQLOutputTypeThe current output type, if known.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.getType();
 
// result contains the getType return value

getParentType()

Returns the current parent composite type.

Signature:

getParentType(): null | undefined | GraphQLCompositeType
Returns
TypeDescription
null | undefined | GraphQLCompositeTypeThe current parent composite type, if known.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.getParentType();
 
// result contains the getParentType return value

getInputType()

Returns the current input type at this point in traversal.

Signature:

getInputType(): null | undefined | GraphQLInputType
Returns
TypeDescription
null | undefined | GraphQLInputTypeThe current input type, if known.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.getInputType();
 
// result contains the getInputType return value

getParentInputType()

Returns the parent input type for the current input position.

Signature:

getParentInputType(): null | undefined | GraphQLInputType
Returns
TypeDescription
null | undefined | GraphQLInputTypeThe parent input type, if known.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.getParentInputType();
 
// result contains the getParentInputType return value

getFieldDef()

Returns the current field definition.

Signature:

getFieldDef(): null | undefined | GraphQLField<unknown, unknown>
Returns
TypeDescription
null | undefined | GraphQLField<unknown, unknown>The current field definition, if known.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.getFieldDef();
 
// result contains the getFieldDef return value

getDefaultValue()

Returns the default value for the current input position.

Signature:

getDefaultValue(): unknown
Returns
TypeDescription
unknownThe current default value, if one is available.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.getDefaultValue();
 
// result contains the getDefaultValue return value

getDirective()

Returns the current directive definition.

Signature:

getDirective(): null | undefined | GraphQLDirective
Returns
TypeDescription
null | undefined | GraphQLDirectiveThe current directive definition, if known.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.getDirective();
 
// result contains the getDirective return value

getArgument()

Returns the current argument definition.

Signature:

getArgument(): null | undefined | GraphQLArgument
Returns
TypeDescription
null | undefined | GraphQLArgumentThe current argument definition, if known.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.getArgument();
 
// result contains the getArgument return value

getEnumValue()

Returns the current enum value definition.

Signature:

getEnumValue(): null | undefined | GraphQLEnumValue
Returns
TypeDescription
null | undefined | GraphQLEnumValueThe current enum value definition, if known.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.getEnumValue();
 
// result contains the getEnumValue return value

enter()

Updates this TypeInfo instance for an entered AST node.

Signature:

enter(node: ASTNode): void
Arguments
NameTypeDescription
nodeASTNodeAST node being entered.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.enter(node);
 
// result contains the enter return value

leave()

Updates this TypeInfo instance for a left AST node.

Signature:

leave(node: ASTNode): void
Arguments
NameTypeDescription
nodeASTNodeAST node being entered.
Example
// Given a TypeInfo instance named typeInfo:
const result = typeInfo.leave(node);
 
// result contains the leave return value

Functions

visitWithTypeInfo()

Creates a new visitor instance which maintains a provided TypeInfo instance along with visiting visitor.

Signature:

visitWithTypeInfo(typeInfo: TypeInfo, visitor: ASTVisitor): ASTVisitor

Arguments

NameTypeDescription
typeInfoTypeInfoOptional type information to reuse during validation.
visitorASTVisitorThe visitor value.

Returns

TypeDescription
ASTVisitorA visitor that updates TypeInfo before and after delegating to the provided visitor.

Example

import { visitWithTypeInfo } from 'graphql/utilities';
 
const result = visitWithTypeInfo(typeInfo, visitor);
 
// result contains the visitWithTypeInfo return value