Class: GraphQL::StaticValidation::FragmentSpreadsArePossible

Inherits:
Object
  • Object
show all
Includes:
Message::MessageHelper
Defined in:
lib/graph_ql/static_validation/rules/fragment_spreads_are_possible.rb

Instance Method Summary collapse

Methods included from Message::MessageHelper

#message

Instance Method Details

#validate(context) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/graph_ql/static_validation/rules/fragment_spreads_are_possible.rb', line 4

def validate(context)

  context.visitor[GraphQL::Nodes::InlineFragment] << -> (node, parent) {
    fragment_parent = context.object_types[-2]
    fragment_child = context.object_types.last
    validate_fragment_in_scope(fragment_parent, fragment_child, node, context)
  }

  spreads_to_validate = []

  context.visitor[GraphQL::Nodes::FragmentSpread] << -> (node, parent) {
    fragment_parent = context.object_types.last
    spreads_to_validate << [node, fragment_parent]
  }

  context.visitor[GraphQL::Nodes::Document].leave << -> (node, parent) {
    spreads_to_validate.each do |spread_values|
      node, fragment_parent = spread_values
      fragment_child_name = context.fragments[node.name].type
      fragment_child = context.schema.types[fragment_child_name]
      validate_fragment_in_scope(fragment_parent, fragment_child, node, context)
    end
  }
end