Class: GraphQL::StaticValidation::ValidationContext
- Inherits:
-
Object
- Object
- GraphQL::StaticValidation::ValidationContext
- Extended by:
- Forwardable
- 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
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#each_irep_node_handlers ⇒ Object
readonly
Returns the value of attribute each_irep_node_handlers.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#visitor ⇒ Object
readonly
Returns the value of attribute visitor.
Instance Method Summary collapse
-
#argument_definition ⇒ GraphQL::Argument?
The most-recently-entered GraphQL::Argument, if currently inside one.
-
#directive_definition ⇒ GraphQL::Directive?
The most-recently-entered GraphQL::Directive, if currently inside one.
-
#each_irep_node(&handler) ⇒ Object
-
#field_definition ⇒ GraphQL::Field?
The most-recently-entered GraphQL::Field, if currently inside one.
-
#initialize(query) ⇒ ValidationContext
constructor
A new instance of ValidationContext.
-
#object_types ⇒ Object
-
#on_dependency_resolve(&handler) ⇒ Object
-
#parent_type_definition ⇒ GraphQL::BaseType
The type which the current type came from.
-
#path ⇒ Array<String>
Field names to get to the current field.
-
#type_definition ⇒ GraphQL::BaseType
The current object type.
-
#valid_literal?(ast_value, type) ⇒ Boolean
Constructor Details
#initialize(query) ⇒ ValidationContext
Returns a new instance of ValidationContext
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/graphql/static_validation/validation_context.rb', line 21 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
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies
17 18 19 |
# File 'lib/graphql/static_validation/validation_context.rb', line 17 def dependencies @dependencies end |
#each_irep_node_handlers ⇒ Object (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 |
#errors ⇒ Object (readonly)
Returns the value of attribute errors
17 18 19 |
# File 'lib/graphql/static_validation/validation_context.rb', line 17 def errors @errors end |
#query ⇒ Object (readonly)
Returns the value of attribute query
17 18 19 |
# File 'lib/graphql/static_validation/validation_context.rb', line 17 def query @query end |
#visitor ⇒ Object (readonly)
Returns the value of attribute visitor
17 18 19 |
# File 'lib/graphql/static_validation/validation_context.rb', line 17 def visitor @visitor end |
Instance Method Details
#argument_definition ⇒ GraphQL::Argument?
Returns The most-recently-entered GraphQL::Argument, if currently inside one
75 76 77 78 79 |
# File 'lib/graphql/static_validation/validation_context.rb', line 75 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 ⇒ GraphQL::Directive?
Returns The most-recently-entered GraphQL::Directive, if currently inside one
70 71 72 |
# File 'lib/graphql/static_validation/validation_context.rb', line 70 def directive_definition @type_stack.directive_definitions.last end |
#each_irep_node(&handler) ⇒ Object
45 46 47 |
# File 'lib/graphql/static_validation/validation_context.rb', line 45 def each_irep_node(&handler) @each_irep_node_handlers << handler end |
#field_definition ⇒ GraphQL::Field?
Returns The most-recently-entered GraphQL::Field, if currently inside one
60 61 62 |
# File 'lib/graphql/static_validation/validation_context.rb', line 60 def field_definition @type_stack.field_definitions.last end |
#object_types ⇒ Object
41 42 43 |
# File 'lib/graphql/static_validation/validation_context.rb', line 41 def object_types @type_stack.object_types end |
#on_dependency_resolve(&handler) ⇒ Object
37 38 39 |
# File 'lib/graphql/static_validation/validation_context.rb', line 37 def on_dependency_resolve(&handler) @on_dependency_resolve_handlers << handler end |
#parent_type_definition ⇒ GraphQL::BaseType
Returns The type which the current type came from
55 56 57 |
# File 'lib/graphql/static_validation/validation_context.rb', line 55 def parent_type_definition object_types[-2] end |
#path ⇒ Array<String>
Returns Field names to get to the current field
65 66 67 |
# File 'lib/graphql/static_validation/validation_context.rb', line 65 def path @type_stack.path.dup end |
#type_definition ⇒ GraphQL::BaseType
Returns The current object type
50 51 52 |
# File 'lib/graphql/static_validation/validation_context.rb', line 50 def type_definition object_types.last end |
#valid_literal?(ast_value, type) ⇒ Boolean
81 82 83 |
# File 'lib/graphql/static_validation/validation_context.rb', line 81 def valid_literal?(ast_value, type) @literal_validator.validate(ast_value, type) end |