Class: GraphQL::StaticValidation::ValidationContext
- Inherits:
-
Object
- Object
- GraphQL::StaticValidation::ValidationContext
- 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
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#fragments ⇒ Object
readonly
Returns the value of attribute fragments.
-
#operations ⇒ Object
readonly
Returns the value of attribute operations.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#visitor ⇒ Object
readonly
Returns the value of attribute visitor.
-
#warden ⇒ Object
readonly
Returns the value of attribute warden.
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.
-
#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
-
#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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/graphql/static_validation/validation_context.rb', line 16 def initialize(query) @query = query @schema = query.schema @document = query.document @fragments = {} @operations = {} @warden = query.warden document.definitions.each do |definition| case definition when GraphQL::Language::Nodes::FragmentDefinition @fragments[definition.name] = definition when GraphQL::Language::Nodes::OperationDefinition @operations[definition.name] = definition end end @errors = [] @visitor = GraphQL::Language::Visitor.new(document) @type_stack = GraphQL::StaticValidation::TypeStack.new(schema, visitor) end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
15 16 17 |
# File 'lib/graphql/static_validation/validation_context.rb', line 15 def document @document end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
15 16 17 |
# File 'lib/graphql/static_validation/validation_context.rb', line 15 def errors @errors end |
#fragments ⇒ Object (readonly)
Returns the value of attribute fragments.
15 16 17 |
# File 'lib/graphql/static_validation/validation_context.rb', line 15 def fragments @fragments end |
#operations ⇒ Object (readonly)
Returns the value of attribute operations.
15 16 17 |
# File 'lib/graphql/static_validation/validation_context.rb', line 15 def operations @operations end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
15 16 17 |
# File 'lib/graphql/static_validation/validation_context.rb', line 15 def query @query end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
15 16 17 |
# File 'lib/graphql/static_validation/validation_context.rb', line 15 def schema @schema end |
#visitor ⇒ Object (readonly)
Returns the value of attribute visitor.
15 16 17 |
# File 'lib/graphql/static_validation/validation_context.rb', line 15 def visitor @visitor end |
#warden ⇒ Object (readonly)
Returns the value of attribute warden.
15 16 17 |
# File 'lib/graphql/static_validation/validation_context.rb', line 15 def warden @warden end |
Instance Method Details
#argument_definition ⇒ GraphQL::Argument?
Returns The most-recently-entered GraphQL::Argument, if currently inside one.
68 69 70 71 72 |
# File 'lib/graphql/static_validation/validation_context.rb', line 68 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.
63 64 65 |
# File 'lib/graphql/static_validation/validation_context.rb', line 63 def directive_definition @type_stack.directive_definitions.last end |
#field_definition ⇒ GraphQL::Field?
Returns The most-recently-entered GraphQL::Field, if currently inside one.
53 54 55 |
# File 'lib/graphql/static_validation/validation_context.rb', line 53 def field_definition @type_stack.field_definitions.last end |
#object_types ⇒ Object
38 39 40 |
# File 'lib/graphql/static_validation/validation_context.rb', line 38 def object_types @type_stack.object_types end |
#parent_type_definition ⇒ GraphQL::BaseType
Returns The type which the current type came from.
48 49 50 |
# File 'lib/graphql/static_validation/validation_context.rb', line 48 def parent_type_definition object_types[-2] end |
#path ⇒ Array<String>
Returns Field names to get to the current field.
58 59 60 |
# File 'lib/graphql/static_validation/validation_context.rb', line 58 def path @type_stack.path.dup end |
#type_definition ⇒ GraphQL::BaseType
Returns The current object type.
43 44 45 |
# File 'lib/graphql/static_validation/validation_context.rb', line 43 def type_definition object_types.last end |
#valid_literal?(ast_value, type) ⇒ Boolean
74 75 76 77 |
# File 'lib/graphql/static_validation/validation_context.rb', line 74 def valid_literal?(ast_value, type) @literal_validator ||= LiteralValidator.new(warden: @warden) @literal_validator.validate(ast_value, type) end |