From 62cf88485b02aabbdc724fc29a63695bcb27a578 Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Wed, 4 Dec 2024 16:26:58 -0500 Subject: [PATCH 01/14] log on unnsuccesful responses --- lib/graphql-hive/client.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index e54c53d..9e077d0 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -24,6 +24,12 @@ def send(path, body, _log_type) request = build_request(uri, body) response = http.request(request) + if response.code.to_i >= 400 && response.code.to_i < 500 + error_message = "Unsuccessful response: #{response.code} - #{response.message}" + extract_error_details(response)&.then { |details| error_message += ": [#{details}]" } + @options[:logger].warn(error_message) + end + @options[:logger].debug(response.inspect) @options[:logger].debug(response.body.inspect) rescue => e @@ -48,6 +54,14 @@ def build_request(uri, body) request.body = JSON.generate(body) request end + + def extract_error_details(response) + parsed_body = JSON.parse(response.body) + return unless parsed_body.is_a?(Hash) && parsed_body['errors'].is_a?(Array) + parsed_body['errors'].map { |error| "path: #{error['path']}, message: #{error['message']}" }.join(", ") + rescue JSON::ParserError + nil + end end end end From a05802e96b38ccb34f072051886a6a58a06e57e7 Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Wed, 4 Dec 2024 16:39:37 -0500 Subject: [PATCH 02/14] lint --- lib/graphql-hive/client.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index 9e077d0..43f6465 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -57,8 +57,8 @@ def build_request(uri, body) def extract_error_details(response) parsed_body = JSON.parse(response.body) - return unless parsed_body.is_a?(Hash) && parsed_body['errors'].is_a?(Array) - parsed_body['errors'].map { |error| "path: #{error['path']}, message: #{error['message']}" }.join(", ") + return unless parsed_body.is_a?(Hash) && parsed_body["errors"].is_a?(Array) + parsed_body["errors"].map { |error| "path: #{error["path"]}, message: #{error["message"]}" }.join(", ") rescue JSON::ParserError nil end From dea3ea9f12d10e7dc0afa27adcd6a0729b441592 Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Wed, 4 Dec 2024 17:10:25 -0500 Subject: [PATCH 03/14] bump to v0.5.3 --- Gemfile.lock | 2 +- lib/graphql-hive/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index e220e00..ec556a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - graphql-hive (0.5.1) + graphql-hive (0.5.3) graphql (>= 2.3, < 3) GEM diff --git a/lib/graphql-hive/version.rb b/lib/graphql-hive/version.rb index 8b2d4b9..42eef1c 100644 --- a/lib/graphql-hive/version.rb +++ b/lib/graphql-hive/version.rb @@ -2,6 +2,6 @@ module Graphql module Hive - VERSION = "0.5.1" + VERSION = "0.5.3" end end From bd49ad304ede1fa6b8d2e87475b785cca2024a89 Mon Sep 17 00:00:00 2001 From: Alyanna Santos <72548456+al-yanna@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:31:10 -0500 Subject: [PATCH 04/14] fix: refactor code Co-authored-by: Arya Bhimani --- lib/graphql-hive/client.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index 43f6465..d02d22c 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -23,8 +23,8 @@ def send(path, body, _log_type) http = setup_http(uri) request = build_request(uri, body) response = http.request(request) - - if response.code.to_i >= 400 && response.code.to_i < 500 + code = response.code.to_i + if code >= 400 && code < 500 error_message = "Unsuccessful response: #{response.code} - #{response.message}" extract_error_details(response)&.then { |details| error_message += ": [#{details}]" } @options[:logger].warn(error_message) From 53df58c75a5206f7608972ae0b7818f36dd12a45 Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Fri, 6 Dec 2024 11:02:19 -0500 Subject: [PATCH 05/14] fix: temp --- lib/graphql-hive/analyzer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/graphql-hive/analyzer.rb b/lib/graphql-hive/analyzer.rb index 0472d44..4b3911f 100644 --- a/lib/graphql-hive/analyzer.rb +++ b/lib/graphql-hive/analyzer.rb @@ -11,7 +11,7 @@ def initialize(query_or_multiplex) def on_enter_field(node, _parent, visitor) parent_type = visitor.parent_type_definition - if parent_type&.respond_to?(:graphql_name) && parent_type.fields[node.name] + if parent_type&.respond_to?(:graphql_name) && node&.respond_to?(:name) @used_fields.add(parent_type.graphql_name) @used_fields.add(make_id(parent_type.graphql_name, node.name)) end From 0ef78094e23df4076f6d90608fc3a2d5a8c3627e Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Fri, 6 Dec 2024 11:04:29 -0500 Subject: [PATCH 06/14] fix: v0.5.4' --- Gemfile.lock | 2 +- lib/graphql-hive/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ec556a5..6e758f0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - graphql-hive (0.5.3) + graphql-hive (0.5.4) graphql (>= 2.3, < 3) GEM diff --git a/lib/graphql-hive/version.rb b/lib/graphql-hive/version.rb index 42eef1c..0af475a 100644 --- a/lib/graphql-hive/version.rb +++ b/lib/graphql-hive/version.rb @@ -2,6 +2,6 @@ module Graphql module Hive - VERSION = "0.5.3" + VERSION = "0.5.4" end end From 7c42ffc51b84aa4739fb0ed31af3b8a37537328d Mon Sep 17 00:00:00 2001 From: Alyanna Santos <72548456+al-yanna@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:41:42 -0500 Subject: [PATCH 07/14] fix: temp log for all responses --- lib/graphql-hive/client.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index d02d22c..f2bfc6f 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -23,6 +23,8 @@ def send(path, body, _log_type) http = setup_http(uri) request = build_request(uri, body) response = http.request(request) + @options[:logger].info("Response: #{response.code} - #{response.message}") + code = response.code.to_i if code >= 400 && code < 500 error_message = "Unsuccessful response: #{response.code} - #{response.message}" From 1e32ffe1312f6da9f3fa733e7e9fa4fe7b7b12e2 Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Mon, 9 Dec 2024 14:54:29 -0500 Subject: [PATCH 08/14] fix: client spec --- spec/graphql/graphql-hive/client_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/graphql/graphql-hive/client_spec.rb b/spec/graphql/graphql-hive/client_spec.rb index 18e8f7a..195c88a 100644 --- a/spec/graphql/graphql-hive/client_spec.rb +++ b/spec/graphql/graphql-hive/client_spec.rb @@ -25,7 +25,7 @@ describe "#send" do let(:http) { instance_double(Net::HTTP) } let(:request) { instance_double(Net::HTTP::Post) } - let(:response) { instance_double(Net::HTTPOK, body: "", code: "200") } + let(:response) { instance_double(Net::HTTPOK, body: "", code: "200", message: "OK") } before do allow(Net::HTTP).to receive(:new).and_return(http) From badda3a28179e4972cc49ebf72df9f90ed1906d2 Mon Sep 17 00:00:00 2001 From: Arya Bhimani Date: Wed, 11 Dec 2024 15:14:31 -0500 Subject: [PATCH 09/14] fix: update logs to info --- lib/graphql-hive/client.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index f2bfc6f..81769e0 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -32,8 +32,8 @@ def send(path, body, _log_type) @options[:logger].warn(error_message) end - @options[:logger].debug(response.inspect) - @options[:logger].debug(response.body.inspect) + @options[:logger].info(response.inspect) + @options[:logger].info(response.body.inspect) rescue => e @options[:logger].fatal("Failed to send data: #{e}") end From b861fe8b2ba05f731270c724b46e28b2a59863ba Mon Sep 17 00:00:00 2001 From: Arya Bhimani Date: Fri, 13 Dec 2024 09:30:04 -0500 Subject: [PATCH 10/14] chore: revert back to debug --- lib/graphql-hive/client.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index 81769e0..f2bfc6f 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -32,8 +32,8 @@ def send(path, body, _log_type) @options[:logger].warn(error_message) end - @options[:logger].info(response.inspect) - @options[:logger].info(response.body.inspect) + @options[:logger].debug(response.inspect) + @options[:logger].debug(response.body.inspect) rescue => e @options[:logger].fatal("Failed to send data: #{e}") end From fd166636e80310d9968c051bb4587cdb6f5858b2 Mon Sep 17 00:00:00 2001 From: Alyanna Santos <72548456+al-yanna@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:46:25 -0500 Subject: [PATCH 11/14] fix: remove temp log --- lib/graphql-hive/client.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index f2bfc6f..bbd9e77 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -23,7 +23,6 @@ def send(path, body, _log_type) http = setup_http(uri) request = build_request(uri, body) response = http.request(request) - @options[:logger].info("Response: #{response.code} - #{response.message}") code = response.code.to_i if code >= 400 && code < 500 From ba628a96183eb35032e5f8c4e8c2d730f2eb9cf1 Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Wed, 18 Dec 2024 17:07:05 -0500 Subject: [PATCH 12/14] feat: add unit tests --- lib/graphql-hive/client.rb | 6 ++-- spec/graphql/graphql-hive/client_spec.rb | 40 +++++++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index bbd9e77..47595d3 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -27,7 +27,7 @@ def send(path, body, _log_type) code = response.code.to_i if code >= 400 && code < 500 error_message = "Unsuccessful response: #{response.code} - #{response.message}" - extract_error_details(response)&.then { |details| error_message += ": [#{details}]" } + error_message += " - #{extract_error_details(response)}" if extract_error_details(response) @options[:logger].warn(error_message) end @@ -59,9 +59,9 @@ def build_request(uri, body) def extract_error_details(response) parsed_body = JSON.parse(response.body) return unless parsed_body.is_a?(Hash) && parsed_body["errors"].is_a?(Array) - parsed_body["errors"].map { |error| "path: #{error["path"]}, message: #{error["message"]}" }.join(", ") + parsed_body["errors"].map { |error| "{ path: #{error["path"]}, message: #{error["message"]} }" }.join(", ") rescue JSON::ParserError - nil + "Could not parse response from Hive" end end end diff --git a/spec/graphql/graphql-hive/client_spec.rb b/spec/graphql/graphql-hive/client_spec.rb index 195c88a..6cec5d5 100644 --- a/spec/graphql/graphql-hive/client_spec.rb +++ b/spec/graphql/graphql-hive/client_spec.rb @@ -68,7 +68,45 @@ it "logs a fatal error when an exception is raised" do allow(http).to receive(:request).and_raise(StandardError.new("Network error")) expect(options[:logger]).to receive(:fatal).with("Failed to send data: Network error") - expect { client.send(:"/usage", body, :usage) }.not_to raise_error(StandardError, "Network error") + expect { client.send(:"/usage", body, :usage) }.not_to raise_error + end + + context "when the response status code is between 400 and 499" do + let(:response) do + instance_double( + Net::HTTPClientError, + body: '{"errors":[{"path":"test1","message":"Error message 1"},{"path":"test2","message":"Error message 2"}]}', + code: "400", + message: "Bad Request" + ) + end + + before do + allow(http).to receive(:request).and_return(response) + end + + it "logs a warning with error details" do + expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request - { path: test1, message: Error message 1 }, { path: test2, message: Error message 2 }") + client.send(:"/usage", body, :usage) + end + + context "when the response body is not valid JSON" do + let(:response) { instance_double(Net::HTTPClientError, body: "Invalid JSON", code: "400", message: "Bad Request") } + + it "logs a warning without error details" do + expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request - Could not parse response from Hive") + client.send(:"/usage", body, :usage) + end + end + + context "when the response body does not contain errors" do + let(:response) { instance_double(Net::HTTPClientError, body: "{}", code: "400", message: "Bad Request") } + + it "logs a warning without error details" do + expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request") + client.send(:"/usage", body, :usage) + end + end end end end From 04f47a1d55742d813ba2a107902d867422e534c9 Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Wed, 18 Dec 2024 17:14:09 -0500 Subject: [PATCH 13/14] feat: revert --- spec/graphql/graphql-hive/client_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/graphql/graphql-hive/client_spec.rb b/spec/graphql/graphql-hive/client_spec.rb index 6cec5d5..0379c4c 100644 --- a/spec/graphql/graphql-hive/client_spec.rb +++ b/spec/graphql/graphql-hive/client_spec.rb @@ -68,7 +68,7 @@ it "logs a fatal error when an exception is raised" do allow(http).to receive(:request).and_raise(StandardError.new("Network error")) expect(options[:logger]).to receive(:fatal).with("Failed to send data: Network error") - expect { client.send(:"/usage", body, :usage) }.not_to raise_error + expect { client.send(:"/usage", body, :usage) }.not_to raise_error(StandardError, "Network error") end context "when the response status code is between 400 and 499" do From d45194e4d87c906380dc55ccdd1b68faf473aeba Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Thu, 19 Dec 2024 09:36:46 -0500 Subject: [PATCH 14/14] feat: refactor --- lib/graphql-hive/client.rb | 3 +-- spec/graphql/graphql-hive/client_spec.rb | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index 47595d3..10dcbeb 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -27,8 +27,7 @@ def send(path, body, _log_type) code = response.code.to_i if code >= 400 && code < 500 error_message = "Unsuccessful response: #{response.code} - #{response.message}" - error_message += " - #{extract_error_details(response)}" if extract_error_details(response) - @options[:logger].warn(error_message) + @options[:logger].warn("#{error_message} #{extract_error_details(response)}") end @options[:logger].debug(response.inspect) diff --git a/spec/graphql/graphql-hive/client_spec.rb b/spec/graphql/graphql-hive/client_spec.rb index 0379c4c..75c9def 100644 --- a/spec/graphql/graphql-hive/client_spec.rb +++ b/spec/graphql/graphql-hive/client_spec.rb @@ -86,7 +86,7 @@ end it "logs a warning with error details" do - expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request - { path: test1, message: Error message 1 }, { path: test2, message: Error message 2 }") + expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request { path: test1, message: Error message 1 }, { path: test2, message: Error message 2 }") client.send(:"/usage", body, :usage) end @@ -94,16 +94,16 @@ let(:response) { instance_double(Net::HTTPClientError, body: "Invalid JSON", code: "400", message: "Bad Request") } it "logs a warning without error details" do - expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request - Could not parse response from Hive") + expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request Could not parse response from Hive") client.send(:"/usage", body, :usage) end end context "when the response body does not contain errors" do - let(:response) { instance_double(Net::HTTPClientError, body: "{}", code: "400", message: "Bad Request") } + let(:response) { instance_double(Net::HTTPClientError, body: "{}", code: "401", message: "Unauthorized") } it "logs a warning without error details" do - expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request") + expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 401 - Unauthorized ") client.send(:"/usage", body, :usage) end end