Class: GraphQL::StaticValidation::FieldsWillMerge

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

Constant Summary collapse

NO_ARGS =

Special handling for fields without arguments

{}.freeze

Instance Method Summary collapse

Instance Method Details

#validate(context) ⇒ Object



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
39
40
41
42
43
44
45
# File 'lib/graphql/static_validation/rules/fields_will_merge.rb', line 8

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|
        if n.arguments.any?
          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
        else
          NO_ARGS
        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