Class: GraphQL::StaticValidation::FragmentSpreadsArePossible

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

Defined Under Namespace

Classes: FragmentSpread

Instance Method Summary collapse

Methods included from Message::MessageHelper

#message

Instance Method Details

#validate(context) ⇒ Object



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
# File 'lib/graphql/static_validation/rules/fragment_spreads_are_possible.rb', line 7

def validate(context)

  context.visitor[GraphQL::Language::Nodes::InlineFragment] << ->(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
  }

  spreads_to_validate = []

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

  context.visitor[GraphQL::Language::Nodes::Document].leave << ->(doc_node, parent) {
    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