Class: GraphQL::StaticValidation::Validator

Inherits:
Object
  • Object
show all
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

Examples:

Validate a query

validator = GraphQL::StaticValidation::Validator.new(schema: MySchema)
document = GraphQL.parse(query_string)
errors = validator.validate(document)

Defined Under Namespace

Classes: Context

Instance Method Summary collapse

Constructor Details

#initialize(schema:, rules: GraphQL::StaticValidation::ALL_RULES) ⇒ Validator

Returns a new instance of Validator.

Parameters:


13
14
15
16
# File 'lib/graphql/static_validation/validator.rb', line 13

def initialize(schema:, rules: GraphQL::StaticValidation::ALL_RULES)
  @schema = schema
  @rules = rules
end

Instance Method Details

#validate(document) ⇒ Array<Hash>

Validate `document` against the schema. Returns an array of message hashes.

Parameters:

Returns:

  • (Array<Hash>)

21
22
23
24
25
26
27
28
# File 'lib/graphql/static_validation/validator.rb', line 21

def validate(document)
  context = Context.new(@schema, document)
  @rules.each do |rules|
    rules.new.validate(context)
  end
  context.visitor.visit(document)
  context.errors.map(&:to_h)
end