Class: GraphQL::StaticValidation::FragmentsAreUsed

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

Defined Under Namespace

Classes: FragmentInstance

Instance Method Summary collapse

Methods included from Message::MessageHelper

#message

Instance Method Details

#validate(context) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/graphql/static_validation/rules/fragments_are_used.rb', line 6

def validate(context)
  v = context.visitor
  used_fragments = []
  defined_fragments = []

  v[GraphQL::Language::Nodes::Document] << ->(node, parent) {
    defined_fragments = node.definitions
      .select { |defn| defn.is_a?(GraphQL::Language::Nodes::FragmentDefinition) }
      .map { |node| FragmentInstance.new(node: node, path: context.path) }
  }

  v[GraphQL::Language::Nodes::FragmentSpread] << ->(node, parent) {
    used_fragments << FragmentInstance.new(node: node, path: context.path)
    if defined_fragments.none? { |defn| defn.name == node.name }
      GraphQL::Language::Visitor::SKIP
    end
  }
  v[GraphQL::Language::Nodes::Document].leave << ->(node, parent) { add_errors(context, used_fragments, defined_fragments) }
end