Sha256: c2eefa4ccd3de552c4087c077dd4aa15293a2ef55504835556abb1b64683a8a3

Contents?: true

Size: 1.41 KB

Versions: 9

Compression:

Stored size: 1.41 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,
          # If there were errors, the irep is garbage
          irep: context.errors.none? ? rewrite.operations : nil,
        }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
graphql-1.2.6 lib/graphql/static_validation/validator.rb
graphql-1.2.5 lib/graphql/static_validation/validator.rb
graphql-1.2.4 lib/graphql/static_validation/validator.rb
graphql-1.2.3 lib/graphql/static_validation/validator.rb
graphql-1.2.2 lib/graphql/static_validation/validator.rb
graphql-1.2.1 lib/graphql/static_validation/validator.rb
graphql-1.2.0 lib/graphql/static_validation/validator.rb
graphql-1.1.0 lib/graphql/static_validation/validator.rb
graphql-1.0.0 lib/graphql/static_validation/validator.rb