Sha256: 288f44d0b4f00ff9d370b895dd80e79d8afd67ad72e70ede10bb3c017825297f
Contents?: true
Size: 1.7 KB
Versions: 9
Compression:
Stored size: 1.7 KB
Contents
# frozen_string_literal: true module GraphQL module StaticValidation # 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 Validator # @param schema [GraphQL::Schema] # @param rules [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 `query` against the schema. Returns an array of message hashes. # @param query [GraphQL::Query] # @return [Array<Hash>] def validate(query) 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) @rules.each do |rules| rules.new.validate(context) 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 end end
Version data entries
9 entries across 9 versions & 1 rubygems