Sha256: ee0de7baa6b92666978813fbc6e5ad34b4a639203b66c2ed855905adf3b4bed8

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 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

        {
          errors: context.errors,
          # If there were errors, the irep is garbage
          irep: context.errors.none? ? rewrite.operations : nil,
        }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
graphql-1.4.5 lib/graphql/static_validation/validator.rb
graphql-1.4.4 lib/graphql/static_validation/validator.rb
graphql-1.4.3 lib/graphql/static_validation/validator.rb
graphql-1.4.2 lib/graphql/static_validation/validator.rb
graphql-1.4.1 lib/graphql/static_validation/validator.rb
graphql-1.4.0 lib/graphql/static_validation/validator.rb
graphql-1.3.0 lib/graphql/static_validation/validator.rb