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.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#max_errors ⇒ Object
readonly
Returns the value of attribute max_errors.
-
#on_dependency_resolve_handlers ⇒ Object
readonly
Returns the value of attribute on_dependency_resolve_handlers.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
-
#visitor ⇒ Object
readonly
Returns the value of attribute visitor.
Instance Method Summary collapse
-
#did_you_mean_suggestion(name, options) ⇒ Object
-
#initialize(query, visitor_class, max_errors) ⇒ ValidationContext
constructor
A new instance of ValidationContext.
-
#on_dependency_resolve(&handler) ⇒ Object
-
#schema_directives ⇒ Object
-
#too_many_errors? ⇒ Boolean
-
#validate_literal(ast_value, type) ⇒ Object
Constructor Details
#initialize(query, visitor_class, max_errors) ⇒ ValidationContext
Returns a new instance of ValidationContext.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/graphql/static_validation/validation_context.rb', line 21 def initialize(query, visitor_class, max_errors) @query = query @types = query.types # TODO update migrated callers to use this accessor @schema = query.schema @literal_validator = LiteralValidator.new(context: query.context) @errors = [] @max_errors = max_errors || Float::INFINITY @on_dependency_resolve_handlers = [] @visitor = visitor_class.new(document, self) end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
14 15 16 |
# File 'lib/graphql/static_validation/validation_context.rb', line 14 def errors @errors end |
#max_errors ⇒ Object (readonly)
Returns the value of attribute max_errors.
14 15 16 |
# File 'lib/graphql/static_validation/validation_context.rb', line 14 def max_errors @max_errors end |
#on_dependency_resolve_handlers ⇒ Object (readonly)
Returns the value of attribute on_dependency_resolve_handlers.
14 15 16 |
# File 'lib/graphql/static_validation/validation_context.rb', line 14 def on_dependency_resolve_handlers @on_dependency_resolve_handlers end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
14 15 16 |
# File 'lib/graphql/static_validation/validation_context.rb', line 14 def query @query end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
14 15 16 |
# File 'lib/graphql/static_validation/validation_context.rb', line 14 def schema @schema end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
14 15 16 |
# File 'lib/graphql/static_validation/validation_context.rb', line 14 def types @types end |
#visitor ⇒ Object (readonly)
Returns the value of attribute visitor.
14 15 16 |
# File 'lib/graphql/static_validation/validation_context.rb', line 14 def visitor @visitor end |
Instance Method Details
#did_you_mean_suggestion(name, options) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/graphql/static_validation/validation_context.rb', line 53 def did_you_mean_suggestion(name, ) if did_you_mean = schema.did_you_mean suggestions = did_you_mean::SpellChecker.new(dictionary: ).correct(name) case suggestions.size when 0 "" when 1 " (Did you mean `#{suggestions.first}`?)" else last_sugg = suggestions.pop " (Did you mean #{suggestions.map {|s| "`#{s}`"}.join(", ")} or `#{last_sugg}`?)" end end 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 |
#schema_directives ⇒ Object
49 50 51 |
# File 'lib/graphql/static_validation/validation_context.rb', line 49 def schema_directives @schema_directives ||= schema.directives end |
#too_many_errors? ⇒ Boolean
45 46 47 |
# File 'lib/graphql/static_validation/validation_context.rb', line 45 def too_many_errors? @errors.length >= @max_errors end |
#validate_literal(ast_value, type) ⇒ Object
41 42 43 |
# File 'lib/graphql/static_validation/validation_context.rb', line 41 def validate_literal(ast_value, type) @literal_validator.validate(ast_value, type) end |