class GraphQL::StaticValidation::ValidationContext

The validation context gets passed to each validator.

It exposes a {GraphQL::Language::Visitor} where validators may add hooks. ({Language::Visitor#visit} is called in {Validator#validate})

It provides access to the schema & fragments which validators may read from.

It holds a list of errors which each validator may add to.

It also provides limited access to the {TypeStack} instance, which tracks state as you climb in and out of different fields.

Attributes

dependencies[R]
document[R]
each_irep_node_handlers[R]
errors[R]
query[R]
schema[R]
visitor[R]
warden[R]

Public Class Methods

new(query) click to toggle source
# File lib/graphql/static_validation/validation_context.rb, line 22
def initialize(query)
  @query = query
  @literal_validator = LiteralValidator.new(context: query.context)
  @errors = []
  @visitor = GraphQL::Language::Visitor.new(document)
  @type_stack = GraphQL::StaticValidation::TypeStack.new(schema, visitor)
  definition_dependencies = DefinitionDependencies.mount(self)
  @on_dependency_resolve_handlers = []
  @each_irep_node_handlers = []
  visitor[GraphQL::Language::Nodes::Document].leave << ->(_n, _p) {
    @dependencies = definition_dependencies.dependency_map { |defn, spreads, frag|
      @on_dependency_resolve_handlers.each { |h| h.call(defn, spreads, frag) }
    }
  }
end

Public Instance Methods

argument_definition() click to toggle source

@return [GraphQL::Argument, nil] The most-recently-entered GraphQL::Argument, if currently inside one

# File lib/graphql/static_validation/validation_context.rb, line 76
def argument_definition
  # Don't get the _last_ one because that's the current one.
  # Get the second-to-last one, which is the parent of the current one.
  @type_stack.argument_definitions[-2]
end
directive_definition() click to toggle source

@return [GraphQL::Directive, nil] The most-recently-entered GraphQL::Directive, if currently inside one

# File lib/graphql/static_validation/validation_context.rb, line 71
def directive_definition
  @type_stack.directive_definitions.last
end
each_irep_node(&handler) click to toggle source
# File lib/graphql/static_validation/validation_context.rb, line 46
def each_irep_node(&handler)
  @each_irep_node_handlers << handler
end
field_definition() click to toggle source

@return [GraphQL::Field, nil] The most-recently-entered GraphQL::Field, if currently inside one

# File lib/graphql/static_validation/validation_context.rb, line 61
def field_definition
  @type_stack.field_definitions.last
end
object_types() click to toggle source
# File lib/graphql/static_validation/validation_context.rb, line 42
def object_types
  @type_stack.object_types
end
on_dependency_resolve(&handler) click to toggle source
# File lib/graphql/static_validation/validation_context.rb, line 38
def on_dependency_resolve(&handler)
  @on_dependency_resolve_handlers << handler
end
parent_type_definition() click to toggle source

@return [GraphQL::BaseType] The type which the current type came from

# File lib/graphql/static_validation/validation_context.rb, line 56
def parent_type_definition
  object_types[-2]
end
path() click to toggle source

@return [Array<String>] Field names to get to the current field

# File lib/graphql/static_validation/validation_context.rb, line 66
def path
  @type_stack.path.dup
end
type_definition() click to toggle source

@return [GraphQL::BaseType] The current object type

# File lib/graphql/static_validation/validation_context.rb, line 51
def type_definition
  object_types.last
end
valid_literal?(ast_value, type) click to toggle source
# File lib/graphql/static_validation/validation_context.rb, line 82
def valid_literal?(ast_value, type)
  @literal_validator.validate(ast_value, type)
end