1 2 3 4 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
# frozen_string_literal: true
module GraphQL
module StaticValidation
module FragmentSpreadsArePossible
def initialize(*)
super
@spreads_to_validate = []
end
def on_inline_fragment(node, parent)
fragment_parent = context.object_types[-2]
fragment_child = context.object_types.last
if fragment_child
validate_fragment_in_scope(fragment_parent, fragment_child, node, context, context.path)
end
super
end
def on_fragment_spread(node, parent)
fragment_parent = context.object_types.last
@spreads_to_validate << FragmentSpread.new(node: node, parent_type: fragment_parent, path: context.path)
super
end
def on_document(node, parent)
super
@spreads_to_validate.each do |frag_spread|
frag_node = context.fragments[frag_spread.node.name]
if frag_node
fragment_child_name = frag_node.type.name
fragment_child = context.warden.get_type(fragment_child_name)
# Might be non-existent type name
if fragment_child
validate_fragment_in_scope(frag_spread.parent_type, fragment_child, frag_spread.node, context, frag_spread.path)
end
end
end
end
private
def validate_fragment_in_scope(parent_type, child_type, node, context, path)
if !child_type.kind.fields?
# It's not a valid fragment type, this error was handled someplace else
return
end
parent_types = context.warden.possible_types(parent_type.unwrap)
child_types = context.warden.possible_types(child_type.unwrap)
if child_types.none? { |c| parent_types.include?(c) }
name = node.respond_to?(:name) ? " #{node.name}" : ""
add_error(GraphQL::StaticValidation::FragmentSpreadsArePossibleError.new(
"Fragment#{name} on #{child_type.graphql_name} can't be spread inside #{parent_type.graphql_name}",
nodes: node,
path: path,
fragment_name: name.empty? ? "unknown" : name,
type: child_type.graphql_name,
parent: parent_type.graphql_name
))
end
end
class FragmentSpread
attr_reader :node, :parent_type, :path
def initialize(node:, parent_type:, path:)
@node = node
@parent_type = parent_type
@path = path
end
end
end
end
end
|