Class: GraphQL::Rubocop::GraphQL::RootTypesInBlock

Inherits:
BaseCop
  • Object
show all
Defined in:
lib/graphql/rubocop/graphql/root_types_in_block.rb

Overview

Identify (and auto-correct) any root types in your schema file.

Examples:

# bad, immediately causes Rails to load `app/graphql/types/query.rb`
query Types::Query

# good, defers loading until the file is needed
query { Types::Query }

Constant Summary collapse

MSG =
"type configuration can be moved to a block to defer loading the type's file"

Instance Method Summary collapse

Methods inherited from BaseCop

#source_without_keyword_argument

Instance Method Details

#on_send(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/graphql/rubocop/graphql/root_types_in_block.rb', line 25

def on_send(node)
  root_type_config_without_block(node) do
    add_offense(node) do |corrector|
      new_node_source = node.source_range.source
      new_node_source.sub!(/(query|mutation|subscription)/, '\1 {')
      new_node_source << " }"
      corrector.replace(node, new_node_source)
    end
  end
end