Method: GraphQL::StaticValidation::Validator#validate
- Defined in:
- lib/graphql/static_validation/validator.rb
#validate(query, validate: true) ⇒ Array<Hash>
Validate query against the schema. Returns an array of message hashes.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/graphql/static_validation/validator.rb', line 24 def validate(query, validate: true) query.trace("validate", { validate: validate, query: query }) do context = GraphQL::StaticValidation::ValidationContext.new(query) rewrite = GraphQL::InternalRepresentation::Rewrite.new # Put this first so its enters and exits are always called rewrite.validate(context) # If the caller opted out of validation, don't attach these if validate @rules.each do |rules| rules.new.validate(context) end end context.visitor.visit # Post-validation: allow validators to register handlers on rewritten query nodes rewrite_result = rewrite.document GraphQL::InternalRepresentation::Visit.visit_each_node(rewrite_result.operation_definitions, context.each_irep_node_handlers) { errors: context.errors, # If there were errors, the irep is garbage irep: context.errors.any? ? nil : rewrite_result, } end end |