Sha256: 8a9f7ca3fde2b89931d16f9b4727725d9ed508244d2b47d86018bb89c40b0d3d

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# Initialized with a {GraphQL::Schema}, then it can validate {GraphQL::Language::Nodes::Documents}s based on that schema.
#
# By default, it's used by {GraphQL::Query}
#
# @example Validate a query
#   validator = GraphQL::StaticValidation::Validator.new(schema: MySchema)
#   document = GraphQL.parse(query_string)
#   errors = validator.validate(document)
#
class GraphQL::StaticValidation::Validator
  # @param schema [GraphQL::Schema]
  # @param rule [Array<#validate(context)>] a list of rules to use when validating
  def initialize(schema:, rules: GraphQL::StaticValidation::ALL_RULES)
    @schema = schema
    @rules = rules
  end

  # Validate `document` against the schema. Returns an array of message hashes.
  # @param document [GraphQL::Language::Nodes::Document]
  # @return [Array<Hash>]
  def validate(query)
    context = GraphQL::StaticValidation::ValidationContext.new(query)
    @rules.each do |rules|
      rules.new.validate(context)
    end
    context.visitor.visit(query.document)
    context.errors.map(&:to_h)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-0.12.1 lib/graphql/static_validation/validator.rb
graphql-0.12.0 lib/graphql/static_validation/validator.rb