Sha256: 03e9cb2cb747e11257e3822f7366ce27bf1c3de315ba5818758a5df795ccbdc8

Contents?: true

Size: 1.16 KB

Versions: 8

Compression:

Stored size: 1.16 KB

Contents

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 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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
graphql-0.15.3 lib/graphql/static_validation/validator.rb
graphql-0.15.2 lib/graphql/static_validation/validator.rb
graphql-0.14.2 lib/graphql/static_validation/validator.rb
graphql-0.15.1 lib/graphql/static_validation/validator.rb
graphql-0.15.0 lib/graphql/static_validation/validator.rb
graphql-0.14.1 lib/graphql/static_validation/validator.rb
graphql-0.14.0 lib/graphql/static_validation/validator.rb
graphql-0.13.0 lib/graphql/static_validation/validator.rb