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
-
#initialize(schema:, rules: GraphQL::StaticValidation::ALL_RULES) ⇒ Validator
constructor
A new instance of Validator.
-
#validate(query, validate: true) ⇒ Array<Hash>
Validate
query
against the schema.
Constructor Details
#initialize(schema:, rules: GraphQL::StaticValidation::ALL_RULES) ⇒ Validator
Returns a new instance of Validator.
16 17 18 19 |
# File 'lib/graphql/static_validation/validator.rb', line 16 def initialize(schema:, rules: GraphQL::StaticValidation::ALL_RULES) @schema = schema @rules = rules end |
Instance Method Details
#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 51 52 53 54 55 56 57 58 59 |
# File 'lib/graphql/static_validation/validator.rb', line 24 def validate(query, validate: true) 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) # Attach legacy-style rules rules_to_use.each do |rule_class_or_module| if rule_class_or_module.method_defined?(:validate) rule_class_or_module.new.validate(context) end end context.visitor.visit 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 end |