-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Rubocop rule for lazy-loaded root types
- Loading branch information
Showing
6 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |