Class: GraphQL::StaticValidation::FragmentNamesAreUnique

Inherits:
Object
  • Object
show all
Includes:
Message::MessageHelper
Defined in:
lib/graphql/static_validation/rules/fragment_names_are_unique.rb

Instance Method Summary collapse

Methods included from Message::MessageHelper

#message

Instance Method Details

#validate(context) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/graphql/static_validation/rules/fragment_names_are_unique.rb', line 7

def validate(context)
  fragments_by_name = Hash.new { |h, k| h[k] = [] }
  context.visitor[GraphQL::Language::Nodes::FragmentDefinition] << ->(node, parent) {
    fragments_by_name[node.name] << node
  }

  context.visitor[GraphQL::Language::Nodes::Document].leave << ->(node, parent) {
    fragments_by_name.each do |name, fragments|
      if fragments.length > 1
        context.errors << message(%|Fragment name "#{name}" must be unique|, fragments, context: context)
      end
    end
  }
end