Module: GraphQL::StaticValidation::DirectivesAreDefined

Defined in:
lib/graphql/static_validation/rules/directives_are_defined.rb

Overview

-- @requires_ancestor: GraphQL::StaticValidation::BaseVisitor

Instance Method Summary collapse

Instance Method Details

#on_directive(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::Directive node, GraphQL::Language::Nodes::AbstractNode? parent) -> void



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphql/static_validation/rules/directives_are_defined.rb', line 10

def on_directive(node, parent)
  if !types.directive_exists?(node.name)
    @directives_are_defined_errors_by_name ||= {} #: Hash[String, GraphQL::StaticValidation::DirectivesAreDefinedError]?
    error = @directives_are_defined_errors_by_name[node.name] ||= begin
      suggestion = if schema.did_you_mean
        @directive_names ||= types.directives.map(&:graphql_name) #: Array[String]?
        context.did_you_mean_suggestion(node.name, @directive_names)
      end
      err = GraphQL::StaticValidation::DirectivesAreDefinedError.new(
        "Directive @#{node.name} is not defined#{suggestion}",
        nodes: [],
        directive: node.name
      )
      add_error(err)
      err
    end
    error.nodes << node
  else
    super
  end
end