Module: GraphQL::StaticValidation::FieldsAreDefinedOnType

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

Instance Method Summary collapse

Instance Method Details

#on_field(node, parent) ⇒ Object



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/graphql/static_validation/rules/fields_are_defined_on_type.rb', line 5

def on_field(node, parent)
  parent_type = @object_types[-2]
  field = context.warden.get_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
      add_error(GraphQL::StaticValidation::FieldsAreDefinedOnTypeError.new(
        "Field '#{node.name}' doesn't exist on type '#{parent_type.graphql_name}'",
        nodes: node,
        field: node.name,
        type: parent_type.graphql_name
      ))
    end
  else
    super
  end
end