From dc0aa0effbf4672be7b5bdff4b636d4a4ff7ed2c Mon Sep 17 00:00:00 2001 From: jmeridth Date: Wed, 28 May 2025 21:28:35 -0500 Subject: [PATCH 1/3] fix: tests Learned we can't get offenses directly off of the cop, it has to be obtained from the commissioner.investigate method. [rubocop sourcecode about not being able to access offenses off the cop base](https://github.com/rubocop/rubocop/blob/9c6e0ef9039f3602cc0b6e267b7be71ed1110d5b/lib/rubocop/cop/base.rb#L314-L318) Learned we have to provide our cop as the forces argument to the commissioner.investigate if we have our own investigate like we do for the overfetch cop. If we don't it never runs. This is not the case with the heredoc cop because it does not have an investigate method. [rubocop commissioner source code where our custom cop invetigate method is actually called](https://github.com/rubocop/rubocop/blob/9c6e0ef9039f3602cc0b6e267b7be71ed1110d5b/lib/rubocop/cop/commissioner.rb#L85) that calls [this function](https://github.com/rubocop/rubocop/blob/9c6e0ef9039f3602cc0b6e267b7be71ed1110d5b/lib/rubocop/cop/commissioner.rb#L163) and you'll notice the second argument is called cops Signed-off-by: jmeridth Co-authored-by: Hannah Yiu --- lib/rubocop/cop/graphql/overfetch.rb | 2 +- test/test_client_fetch.rb | 4 +- test/test_rubocop_heredoc.rb | 65 ++++++++++++++-------------- test/test_rubocop_overfetch.rb | 17 ++++---- 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/lib/rubocop/cop/graphql/overfetch.rb b/lib/rubocop/cop/graphql/overfetch.rb index 182fe838..f30f1bd2 100644 --- a/lib/rubocop/cop/graphql/overfetch.rb +++ b/lib/rubocop/cop/graphql/overfetch.rb @@ -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 diff --git a/test/test_client_fetch.rb b/test/test_client_fetch.rb index e438f701..dbf99a4c 100644 --- a/test/test_client_fetch.rb +++ b/test/test_client_fetch.rb @@ -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 diff --git a/test/test_rubocop_heredoc.rb b/test/test_rubocop_heredoc.rb index e1a55868..57fd0115 100644 --- a/test/test_rubocop_heredoc.rb +++ b/test/test_rubocop_heredoc.rb @@ -9,85 +9,85 @@ 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 < Date: Wed, 28 May 2025 22:10:54 -0500 Subject: [PATCH 2/3] fix: add logger require statement Signed-off-by: jmeridth --- lib/graphql/client/erb.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/graphql/client/erb.rb b/lib/graphql/client/erb.rb index d4b42fd3..d549f29c 100644 --- a/lib/graphql/client/erb.rb +++ b/lib/graphql/client/erb.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true require "action_view" +require "logger" module GraphQL class Client From f1068b97e8c2b97f01667292769a98f97ee52bb1 Mon Sep 17 00:00:00 2001 From: jmeridth Date: Thu, 29 May 2025 08:43:52 -0500 Subject: [PATCH 3/3] fix: use specific version of concurrent-ruby gem According to [this stackoverflow post](https://stackoverflow.com/a/79361034) by using the 1.3.4 version of concurrent-ruby gem, this should fix our logging issues. Signed-off-by: jmeridth --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 009240b6..5b908a99 100644 --- a/Gemfile +++ b/Gemfile @@ -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