Class: GraphQL::StaticValidation::ValidationContext

Inherits:
Object
  • Object
show all
Extended by:
Delegate
Defined in:
lib/graphql/static_validation/validation_context.rb

Overview

The validation context gets passed to each validator.

It exposes a Language::Visitor where validators may add hooks. (Language::Visitor#visit is called in GraphQL::StaticValidation::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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegate

def_delegators

Constructor Details

#initialize(query) ⇒ ValidationContext

Returns a new instance of ValidationContext



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/graphql/static_validation/validation_context.rb', line 23

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

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies



17
18
19
# File 'lib/graphql/static_validation/validation_context.rb', line 17

def dependencies
  @dependencies
end

#documentObject (readonly)

Returns the value of attribute document



17
18
19
# File 'lib/graphql/static_validation/validation_context.rb', line 17

def document
  @document
end

#each_irep_node_handlersObject (readonly)

Returns the value of attribute each_irep_node_handlers



17
18
19
# File 'lib/graphql/static_validation/validation_context.rb', line 17

def each_irep_node_handlers
  @each_irep_node_handlers
end

#errorsObject (readonly)

Returns the value of attribute errors



17
18
19
# File 'lib/graphql/static_validation/validation_context.rb', line 17

def errors
  @errors
end

#queryObject (readonly)

Returns the value of attribute query



17
18
19
# File 'lib/graphql/static_validation/validation_context.rb', line 17

def query
  @query
end

#schemaObject (readonly)

Returns the value of attribute schema



17
18
19
# File 'lib/graphql/static_validation/validation_context.rb', line 17

def schema
  @schema
end

#visitorObject (readonly)

Returns the value of attribute visitor



17
18
19
# File 'lib/graphql/static_validation/validation_context.rb', line 17

def visitor
  @visitor
end

#wardenObject (readonly)

Returns the value of attribute warden



17
18
19
# File 'lib/graphql/static_validation/validation_context.rb', line 17

def warden
  @warden
end

Instance Method Details

#argument_definitionGraphQL::Argument?

Returns The most-recently-entered GraphQL::Argument, if currently inside one

Returns:

  • (GraphQL::Argument, nil)

    The most-recently-entered GraphQL::Argument, if currently inside one



77
78
79
80
81
# File 'lib/graphql/static_validation/validation_context.rb', line 77

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_definitionGraphQL::Directive?

Returns The most-recently-entered GraphQL::Directive, if currently inside one

Returns:

  • (GraphQL::Directive, nil)

    The most-recently-entered GraphQL::Directive, if currently inside one



72
73
74
# File 'lib/graphql/static_validation/validation_context.rb', line 72

def directive_definition
  @type_stack.directive_definitions.last
end

#each_irep_node(&handler) ⇒ Object



47
48
49
# File 'lib/graphql/static_validation/validation_context.rb', line 47

def each_irep_node(&handler)
  @each_irep_node_handlers << handler
end

#field_definitionGraphQL::Field?

Returns The most-recently-entered GraphQL::Field, if currently inside one

Returns:

  • (GraphQL::Field, nil)

    The most-recently-entered GraphQL::Field, if currently inside one



62
63
64
# File 'lib/graphql/static_validation/validation_context.rb', line 62

def field_definition
  @type_stack.field_definitions.last
end

#object_typesObject



43
44
45
# File 'lib/graphql/static_validation/validation_context.rb', line 43

def object_types
  @type_stack.object_types
end

#on_dependency_resolve(&handler) ⇒ Object



39
40
41
# File 'lib/graphql/static_validation/validation_context.rb', line 39

def on_dependency_resolve(&handler)
  @on_dependency_resolve_handlers << handler
end

#parent_type_definitionGraphQL::BaseType

Returns The type which the current type came from

Returns:



57
58
59
# File 'lib/graphql/static_validation/validation_context.rb', line 57

def parent_type_definition
  object_types[-2]
end

#pathArray<String>

Returns Field names to get to the current field

Returns:

  • (Array<String>)

    Field names to get to the current field



67
68
69
# File 'lib/graphql/static_validation/validation_context.rb', line 67

def path
  @type_stack.path.dup
end

#type_definitionGraphQL::BaseType

Returns The current object type

Returns:



52
53
54
# File 'lib/graphql/static_validation/validation_context.rb', line 52

def type_definition
  object_types.last
end

#valid_literal?(ast_value, type) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/graphql/static_validation/validation_context.rb', line 83

def valid_literal?(ast_value, type)
  @literal_validator.validate(ast_value, type)
end