Module: GraphQL::StaticValidation::FieldsAreDefinedOnType

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#on_field(node, parent) ⇒ Object

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



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

def on_field(node, parent)
  # Fields are only visited inside a known type (other rules stop the visit otherwise)
  parent_type = parent_type_definition #: as !nil
  field = context.types.field(parent_type, node.name)

  if field.nil?
    if parent_type.kind.union?
      add_error(GraphQL::StaticValidation::FieldsHaveAppropriateSelectionsError.new(
        "Selections can't be made directly on unions (see selections on #{parent_type.graphql_name})",
        nodes: parent,
        node_name: parent_type.graphql_name
      ))
    else
      suggestion = if schema.did_you_mean
        context.did_you_mean_suggestion(node.name, possible_fields(context, parent_type))
      end
      message = "Field '#{node.name}' doesn't exist on type '#{parent_type.graphql_name}'#{suggestion}"
      add_error(GraphQL::StaticValidation::FieldsAreDefinedOnTypeError.new(
        message,
        nodes: node,
        field: node.name,
        type: parent_type.graphql_name
      ))
    end
  else
    super
  end
end