Class: GraphQL::StaticValidation::Validator
- Inherits:
-
Object
- Object
- GraphQL::StaticValidation::Validator
- Defined in:
- lib/graphql/static_validation/validator.rb
Overview
Initialized with a GraphQL::Schema, then it can validate Language::Nodes::Documentss based on that schema.
By default, it’s used by Query
Instance Method Summary collapse
-
#handle_timeout(query, context) ⇒ Object
Invoked when static validation times out.
-
#initialize(schema:, rules: GraphQL::StaticValidation::ALL_RULES) ⇒ Validator
constructor
A new instance of Validator.
-
#validate(query, validate: true, timeout: nil, max_errors: nil) ⇒ Array<Hash>
Validate
query
against the schema.
Constructor Details
#initialize(schema:, rules: GraphQL::StaticValidation::ALL_RULES) ⇒ Validator
Returns a new instance of Validator.
18 19 20 21 |
# File 'lib/graphql/static_validation/validator.rb', line 18 def initialize(schema:, rules: GraphQL::StaticValidation::ALL_RULES) @schema = schema @rules = rules end |
Instance Method Details
#handle_timeout(query, context) ⇒ Object
Invoked when static validation times out.
89 90 91 92 93 |
# File 'lib/graphql/static_validation/validator.rb', line 89 def handle_timeout(query, context) context.errors << GraphQL::StaticValidation::ValidationTimeoutError.new( "Timeout on validation of query" ) end |
#validate(query, validate: true, timeout: nil, max_errors: nil) ⇒ Array<Hash>
Validate query
against the schema. Returns an array of message hashes.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/graphql/static_validation/validator.rb', line 29 def validate(query, validate: true, timeout: nil, max_errors: nil) query.trace("validate", { validate: validate, query: query }) do can_skip_rewrite = query.context.interpreter? && query.schema.using_ast_analysis? && query.schema.is_a?(Class) errors = if validate == false && can_skip_rewrite [] else rules_to_use = validate ? @rules : [] visitor_class = BaseVisitor.including_rules(rules_to_use, rewrite: !can_skip_rewrite) context = GraphQL::StaticValidation::ValidationContext.new(query, visitor_class, max_errors) begin # CAUTION: Usage of the timeout module makes the assumption that validation rules are stateless Ruby code that requires no cleanup if process was interrupted. This means no blocking IO calls, native gems, locks, or `rescue` clauses that must be reached. # A timeout value of 0 or nil will execute the block without any timeout. Timeout::timeout(timeout) do catch(:too_many_validation_errors) do # Attach legacy-style rules. # Only loop through rules if it has legacy-style rules unless (legacy_rules = rules_to_use - GraphQL::StaticValidation::ALL_RULES).empty? legacy_rules.each do |rule_class_or_module| if rule_class_or_module.method_defined?(:validate) GraphQL::Deprecation.warn "Legacy validator rules will be removed from GraphQL-Ruby 2.0, use a module instead (see the built-in rules: https://github.com/rmosolgo/graphql-ruby/tree/master/lib/graphql/static_validation/rules)" GraphQL::Deprecation.warn " -> Legacy validator: #{rule_class_or_module}" rule_class_or_module.new.validate(context) end end end context.visitor.visit end end rescue Timeout::Error handle_timeout(query, context) end context.errors end irep = if errors.empty? && context # Only return this if there are no errors and validation was actually run context.visitor.rewrite_document else nil end { errors: errors, irep: irep, } end rescue GraphQL::ExecutionError => e { errors: [e], irep: nil, } end |