7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/graphql/static_validation/rules/operation_names_are_valid.rb', line 7
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
|