Sha256: fa78fc85fac15a4a9cd0517b0bd1e52f899ff72cd3c8152869e8f89a569e0458

Contents?: true

Size: 1.42 KB

Versions: 26

Compression:

Stored size: 1.42 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 `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.map(&:to_h),
          # If there were errors, the irep is garbage
          irep: context.errors.none? ? rewrite.operations : nil,
        }
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
graphql-0.19.4 lib/graphql/static_validation/validator.rb
graphql-0.19.3 lib/graphql/static_validation/validator.rb
graphql-0.19.2 lib/graphql/static_validation/validator.rb
graphql-0.19.1 lib/graphql/static_validation/validator.rb
graphql-0.19.0 lib/graphql/static_validation/validator.rb
graphql-0.18.15 lib/graphql/static_validation/validator.rb
graphql-0.18.14 lib/graphql/static_validation/validator.rb
graphql-0.18.13 lib/graphql/static_validation/validator.rb
graphql-0.18.12 lib/graphql/static_validation/validator.rb
graphql-0.18.11 lib/graphql/static_validation/validator.rb
graphql-0.18.10 lib/graphql/static_validation/validator.rb
graphql-0.18.9 lib/graphql/static_validation/validator.rb
graphql-0.18.8 lib/graphql/static_validation/validator.rb
graphql-0.18.7 lib/graphql/static_validation/validator.rb
graphql-0.18.6 lib/graphql/static_validation/validator.rb
graphql-0.18.5 lib/graphql/static_validation/validator.rb
graphql-0.18.4 lib/graphql/static_validation/validator.rb
graphql-0.18.3 lib/graphql/static_validation/validator.rb
graphql-0.18.2 lib/graphql/static_validation/validator.rb
graphql-0.18.1 lib/graphql/static_validation/validator.rb