Method: GraphQL::StaticValidation::Validator#validate

Defined in:
lib/graphql/static_validation/validator.rb

#validate(query, validate: true, timeout: nil, max_errors: nil) ⇒ Array<Hash>

Validate query against the schema. Returns an array of message hashes.

Parameters:

  • query (GraphQL::Query)
  • validate (Boolean) (defaults to: true)
  • timeout (Float) (defaults to: nil)

    Number of seconds to wait before aborting validation. Any positive number may be used, including Floats to specify fractional seconds.

  • max_errors (Integer) (defaults to: nil)

    Maximum number of errors before aborting validation. Any positive number will limit the number of errors. Defaults to nil for no limit.

Returns:

  • (Array<Hash>)


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
# File 'lib/graphql/static_validation/validator.rb', line 29

def validate(query, validate: true, timeout: nil, max_errors: nil)
  query.current_trace.validate(validate: validate, query: query) do
    errors = if validate == false
      []
    else
      rules_to_use = validate ? @rules : []
      visitor_class = BaseVisitor.including_rules(rules_to_use)

      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
            context.visitor.visit
          end
        end
      rescue Timeout::Error
        handle_timeout(query, context)
      end

      context.errors
    end

    {
      errors: errors,
    }
  end
rescue GraphQL::ExecutionError => e
  {
    errors: [e],
  }
end