Module: GraphQL::StaticValidation::FragmentNamesAreUnique

Defined in:
lib/graphql/static_validation/rules/fragment_names_are_unique.rb

Overview

-- @requires_ancestor: GraphQL::StaticValidation::BaseVisitor

Instance Method Summary collapse

Instance Method Details

#initialize(document, context) ⇒ Object

: (GraphQL::Language::Nodes::Document document, GraphQL::StaticValidation::ValidationContext context) -> void



10
11
12
13
# File 'lib/graphql/static_validation/rules/fragment_names_are_unique.rb', line 10

def initialize(document, context)
  super
  @fragments_by_name = Hash.new { |h, k| h[k] = [] } #: Hash[String, Array[GraphQL::Language::Nodes::FragmentDefinition]]
end

#on_document(_n, _p) ⇒ Object

: (GraphQL::Language::Nodes::Document _n, GraphQL::Language::Nodes::AbstractNode? _p) -> void



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/graphql/static_validation/rules/fragment_names_are_unique.rb', line 24

def on_document(_n, _p)
  super
  @fragments_by_name.each do |name, fragments|
    if fragments.length > 1
      add_error(GraphQL::StaticValidation::FragmentNamesAreUniqueError.new(
        %|Fragment name "#{name}" must be unique|,
        nodes: fragments,
        name: name
      ))
    end
  end
end

#on_fragment_definition(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::FragmentDefinition node, GraphQL::Language::Nodes::AbstractNode? parent) -> void



16
17
18
19
20
21
# File 'lib/graphql/static_validation/rules/fragment_names_are_unique.rb', line 16

def on_fragment_definition(node, parent)
  # The default proc creates the Array:
  fragments = @fragments_by_name[node.name] #: as !nil
  fragments << node
  super
end