Class: GraphQL::StaticValidation::FieldsWillMerge

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/static_validation/rules/fields_will_merge.rb

Instance Method Summary collapse

Instance Method Details

#validate(context) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphql/static_validation/rules/fields_will_merge.rb', line 5

def validate(context)
  context.each_irep_node do |node|
    if node.ast_nodes.size > 1
      defn_names = Set.new(node.ast_nodes.map(&:name))

      # Check for more than one GraphQL::Field backing this node:
      if defn_names.size > 1
        defn_names = defn_names.sort.join(" or ")
        msg = "Field '#{node.name}' has a field conflict: #{defn_names}?"
        context.errors << GraphQL::StaticValidation::Message.new(msg, nodes: node.ast_nodes.to_a)
      end

      # Check for incompatible / non-identical arguments on this node:
      args = node.ast_nodes.map do |n|
        n.arguments.reduce({}) do |memo, a|
          arg_value = a.value
          memo[a.name] = case arg_value
          when GraphQL::Language::Nodes::AbstractNode
            arg_value.to_query_string
          else
            GraphQL::Language.serialize(arg_value)
          end
          memo
        end
      end
      args.uniq!

      if args.length > 1
        msg = "Field '#{node.name}' has an argument conflict: #{args.map{ |arg| GraphQL::Language.serialize(arg) }.join(" or ")}?"
        context.errors << GraphQL::StaticValidation::Message.new(msg, nodes: node.ast_nodes.to_a)
      end
    end
  end
end