Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gemspec
rails_version = ENV["RAILS_VERSION"] == "edge" ? { github: "rails/rails" } : ENV["RAILS_VERSION"]
gem "actionpack", rails_version
gem "activesupport", rails_version
gem "concurrent-ruby", "1.3.4"

graphql_version = ENV["GRAPHQL_VERSION"] == "edge" ? { github: "rmosolgo/graphql-ruby", ref: "interpreter-without-legacy" } : ENV["GRAPHQL_VERSION"]
gem "graphql", graphql_version
Expand Down
1 change: 1 addition & 0 deletions lib/graphql/client/erb.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require "action_view"
require "logger"

module GraphQL
class Client
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/graphql/overfetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def investigate(processed_source)

visitor.fields.each do |field, count|
next if count > 0
add_offense(nil, message: "GraphQL field '#{field}' query but was not used in template.")
add_offense(visitor.ranges[field], message: "GraphQL field '#{field}' query but was not used in template.")
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/test_client_fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ def self.resolve_type(_type, _obj, _ctx)
refute response.data

refute_empty response.errors
assert_equal "Field 'err' doesn't exist on type 'Query'", response.errors[:data][0]
assert_includes response.errors[:data][0], "Field 'err' doesn't exist on type 'Query'"

refute_empty response.errors.all
assert_equal "Field 'err' doesn't exist on type 'Query'", response.errors[:data][0]
assert_includes response.errors[:data][0], "Field 'err' doesn't exist on type 'Query'"
end

def test_failed_response
Expand Down
65 changes: 32 additions & 33 deletions test/test_rubocop_heredoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,126 +9,126 @@ def setup
end

def test_good_graphql_heredoc
investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
Query = Client.parse <<'GRAPHQL'
{ version }
GRAPHQL
RUBY

assert_empty @cop.offenses.map(&:message)
assert_empty result.offenses.map(&:message)
end

def test_good_graphql_dash_heredoc
investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
Query = Client.parse <<-'GRAPHQL'
{ version }
GRAPHQL
RUBY

assert_empty @cop.offenses.map(&:message)
assert_empty result.offenses.map(&:message)
end

def test_good_graphql_squiggly_heredoc
investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
Query = Client.parse <<~'GRAPHQL'
{ version }
GRAPHQL
RUBY

assert_empty @cop.offenses.map(&:message)
assert_empty result.offenses.map(&:message)
end

def test_bad_graphql_heredoc
investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
Query = Client.parse <<GRAPHQL
{ version }
GRAPHQL
RUBY

assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses.first.message
assert_equal 1, result.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses.first.message
end

def test_bad_graphql_dash_heredoc
investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
Query = Client.parse <<-GRAPHQL
{ version }
GRAPHQL
RUBY

assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses.first.message
assert_equal 1, result.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses.first.message
end

def test_bad_graphql_squiggly_heredoc
skip if RUBY_VERSION < "2.3"

investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
Query = Client.parse <<~GRAPHQL
{ version }
GRAPHQL
RUBY

assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses.first.message
assert_equal 1, result.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses.first.message
end

def test_bad_graphql_heredoc_with_interpolation
investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
field = "version"
Query = Client.parse <<-GRAPHQL
{ \#{field} }
GRAPHQL
RUBY

assert_equal 2, @cop.offenses.count
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", @cop.offenses[0].message
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[1].message
assert_equal 2, result.offenses.count
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", result.offenses[0].message
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[1].message
end

def test_bad_graphql_multiline_heredoc
investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
Query = Client.parse <<GRAPHQL
{
version
}
GRAPHQL
RUBY

assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[0].message
assert_equal 1, result.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[0].message
end

def test_bad_graphql_multiline_dash_heredoc
investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
Query = Client.parse <<-GRAPHQL
{
version
}
GRAPHQL
RUBY

assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[0].message
assert_equal 1, result.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[0].message
end

def test_bad_graphql_multiline_squiggly_heredoc
skip if RUBY_VERSION < "2.3"

investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
Query = Client.parse <<~GRAPHQL
{
version
}
GRAPHQL
RUBY

assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[0].message
assert_equal 1, result.offenses.count
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[0].message
end

def test_bad_graphql_multiline_heredoc_with_interpolation
investigate(@cop, <<-RUBY)
result = investigate(@cop, <<-RUBY)
field = "version"
Query = Client.parse <<-GRAPHQL
{
Expand All @@ -137,9 +137,9 @@ def test_bad_graphql_multiline_heredoc_with_interpolation
GRAPHQL
RUBY

assert_equal 2, @cop.offenses.count
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", @cop.offenses[0].message
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[1].message
assert_equal 2, result.offenses.count
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", result.offenses[0].message
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[1].message
end

private
Expand All @@ -148,6 +148,5 @@ def investigate(cop, src)
processed_source = RuboCop::ProcessedSource.new(src, RUBY_VERSION.to_f)
commissioner = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
commissioner.investigate(processed_source)
commissioner
end
end
17 changes: 8 additions & 9 deletions test/test_rubocop_overfetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,32 @@ def setup
end

def test_all_fields_used
investigate(@cop, "#{Root}/views/users/show.html.erb")
result = investigate(@cop, "#{Root}/views/users/show.html.erb")

assert_empty @cop.offenses.map(&:message)
assert_empty result.offenses.map(&:message)
end

def test_all_fields_used_with_safe_navigation_operator
skip if RUBY_VERSION < "2.3"

investigate(@cop, "#{Root}/views/users/show-2-3.html.erb")
result = investigate(@cop, "#{Root}/views/users/show-2-3.html.erb")

assert_empty @cop.offenses.map(&:message)
assert_empty result.offenses.map(&:message)
end

def test_field_unused
investigate(@cop, "#{Root}/views/users/overfetch.html.erb")
result = investigate(@cop, "#{Root}/views/users/overfetch.html.erb")

assert_equal 1, @cop.offenses.count
assert_equal "GraphQL/Overfetch: GraphQL field 'birthday' query but was not used in template.", @cop.offenses.first.message
assert_equal 1, result.offenses.count
assert_equal "GraphQL/Overfetch: GraphQL field 'birthday' query but was not used in template.", result.offenses.first.message
end

private

def investigate(cop, path)
engine = GraphQL::Client::ERB.new(File.read(path))
processed_source = RuboCop::ProcessedSource.new(engine.src.dup, RUBY_VERSION.to_f, path)
commissioner = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
commissioner = RuboCop::Cop::Commissioner.new([cop], [cop], raise_error: true)
commissioner.investigate(processed_source)
commissioner
end
end