Skip to content

Commit

Permalink
Add a Rubocop rule for lazy-loaded root types
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Aug 8, 2024
1 parent 9675002 commit b3dc381
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/graphql/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
require "graphql/rubocop/graphql/default_null_true"
require "graphql/rubocop/graphql/default_required_true"
require "graphql/rubocop/graphql/field_type_in_block"
require "graphql/rubocop/graphql/root_types_in_block"
38 changes: 38 additions & 0 deletions lib/graphql/rubocop/graphql/root_types_in_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true
require_relative "./base_cop"

module GraphQL
module Rubocop
module GraphQL
# Identify (and auto-correct) any root types in your schema file.
#
# @example
# # 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 }
#
class RootTypesInBlock < BaseCop
MSG = "type configuration can be moved to a block to defer loading the type's file"

def_node_matcher :root_type_config_without_block, <<-Pattern
(
send nil? {:query :mutation :subscription} const
)
Pattern

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
end
end
end
end
6 changes: 6 additions & 0 deletions spec/fixtures/cop/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ GraphQL/FieldTypeInBlock:
Include:
- field_type.rb
- field_type_autocorrect.rb

GraphQL/RootTypesInBlock:
Enabled: true
Include:
- root_types.rb
- root_types_autocorrect.rb
5 changes: 5 additions & 0 deletions spec/fixtures/cop/root_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MyAppSchema < GraphQL::Schema
query Types::Query
mutation Types::Mutation
subscription Types::Subscription
end
5 changes: 5 additions & 0 deletions spec/fixtures/cop/root_types_corrected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MyAppSchema < GraphQL::Schema
query { Types::Query }
mutation { Types::Mutation }
subscription { Types::Subscription }
end
29 changes: 29 additions & 0 deletions spec/graphql/cop/root_types_in_block_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true
require 'spec_helper'

describe "GraphQL::Cop::RootTypesInBlock" do
include RubocopTestHelpers

it "finds and autocorrects field corrections with inline types" do
result = run_rubocop_on("spec/fixtures/cop/root_types.rb")
puts result
assert_equal 3, rubocop_errors(result)

assert_includes result, <<-RUBY
query Types::Query
^^^^^^^^^^^^^^^^^^
RUBY

assert_includes result, <<-RUBY
mutation Types::Mutation
^^^^^^^^^^^^^^^^^^^^^^^^
RUBY

assert_includes result, <<-RUBY
subscription Types::Subscription
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RUBY

assert_rubocop_autocorrects_all("spec/fixtures/cop/root_types.rb")
end
end

0 comments on commit b3dc381

Please sign in to comment.