Sha256: a8cba6e8b99d653cda3e110616a728b5b1027d47c1aa68ef3fac578d32ee09c2

Contents?: true

Size: 982 Bytes

Versions: 65

Compression:

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

65 entries across 65 versions & 1 rubygems

Version Path
graphql-1.8.18 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.17 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.16 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.15 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.14 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.13 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.12 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.11 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.10 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.9 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.8 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.7 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.6 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.5 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.4 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.3 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.2 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.1 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.0 lib/graphql/static_validation/rules/operation_names_are_valid.rb
graphql-1.8.0.pre11 lib/graphql/static_validation/rules/operation_names_are_valid.rb