Sha256: 29ce1c096c114ef7d17dd12761bd40f7e96b1d2c86a9894e699de8d575210dfe

Contents?: true

Size: 983 Bytes

Versions: 3

Compression:

Stored size: 983 Bytes

Contents

# frozen_string_literal: true
module GraphQL
  module StaticValidation
    class OperationNamesAreValid
      include GraphQL::StaticValidation::Message::MessageHelper

      def validate(context)
        op_names = Hash.new { |h, k| h[k] = [] }

        context.visitor[GraphQL::Language::Nodes::OperationDefinition].enter << ->(node, _parent) {
          op_names[node.name] << node
        }

        context.visitor[GraphQL::Language::Nodes::Document].leave << ->(node, _parent) {
          op_count = op_names.values.inject(0) { |m, v| m += v.size }

          op_names.each do |name, nodes|
            if name.nil? && op_count > 1
              context.errors << message(%|Operation name is required when multiple operations are present|, nodes, context: context)
            elsif nodes.length > 1
              context.errors << message(%|Operation name "#{name}" must be unique|, nodes, context: context)
            end
          end
        }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graphql-1.5.5 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.5.4 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.5.3 lib/graphql/static_validation/rules/operation_names_are_valid.rb