From 1d42e197d103fca93da84b6030ed636e83381dac Mon Sep 17 00:00:00 2001 From: billybonks Date: Sun, 10 Nov 2024 15:24:35 +0800 Subject: [PATCH 1/2] feat: Enable consumers to access the full response payload Update the Response public API object to include full_response. this is non-breaking. Passing the full response from the executing object to the client is currently done via the client checking with the object, versus it being part of the response, In order to change this it requires changing the much more changes, as all the tests are using GraphQL::Schema for the execution of the graphql --- lib/graphql/client.rb | 5 +++-- lib/graphql/client/http.rb | 6 ++++++ lib/graphql/client/response.rb | 8 +++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/graphql/client.rb b/lib/graphql/client.rb index f064ca2e..37bbe83b 100644 --- a/lib/graphql/client.rb +++ b/lib/graphql/client.rb @@ -380,12 +380,13 @@ def query(definition, variables: {}, context: {}) error_payload = payload.merge(message: error["message"], error: error) ActiveSupport::Notifications.instrument("error.graphql", error_payload) end - + Response.new( result, data: definition.new(data, Errors.new(errors, ["data"])), errors: Errors.new(errors), - extensions: extensions + extensions:, + full_response: execute.respond_to?("last_response") ? execute.last_response : nil ) end diff --git a/lib/graphql/client/http.rb b/lib/graphql/client/http.rb index 700aaa8f..7904ce12 100644 --- a/lib/graphql/client/http.rb +++ b/lib/graphql/client/http.rb @@ -45,6 +45,11 @@ def headers(_context) {} end + # Public: full reponse from last request + # + # Returns Hash. + attr_reader :last_response + # Public: Make an HTTP request for GraphQL query. # # Implements Client's "execute" adapter interface. @@ -71,6 +76,7 @@ def execute(document:, operation_name: nil, variables: {}, context: {}) request.body = JSON.generate(body) response = connection.request(request) + @last_response = response.to_hash case response when Net::HTTPOK, Net::HTTPBadRequest JSON.parse(response.body) diff --git a/lib/graphql/client/response.rb b/lib/graphql/client/response.rb index f2624180..c00e250a 100644 --- a/lib/graphql/client/response.rb +++ b/lib/graphql/client/response.rb @@ -31,12 +31,18 @@ class Response # Public: Hash of server specific extension metadata. attr_reader :extensions + # Public: Complete response hash returned from server. + # + # Returns Hash + attr_reader :full_response + # Internal: Initialize base class. - def initialize(hash, data: nil, errors: Errors.new, extensions: {}) + def initialize(hash, data: nil, errors: Errors.new, extensions: {}, full_response: nil) @original_hash = hash @data = data @errors = errors @extensions = extensions + @full_response = full_response end end end From ca63d53523cc29a6704ad9f05d83b882fcb9c78a Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Wed, 13 Nov 2024 08:26:58 -0500 Subject: [PATCH 2/2] Apply suggestions from code review --- lib/graphql/client.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/graphql/client.rb b/lib/graphql/client.rb index 37bbe83b..6fea76d1 100644 --- a/lib/graphql/client.rb +++ b/lib/graphql/client.rb @@ -380,12 +380,11 @@ def query(definition, variables: {}, context: {}) error_payload = payload.merge(message: error["message"], error: error) ActiveSupport::Notifications.instrument("error.graphql", error_payload) end - Response.new( result, data: definition.new(data, Errors.new(errors, ["data"])), errors: Errors.new(errors), - extensions:, + extensions: extensions, full_response: execute.respond_to?("last_response") ? execute.last_response : nil ) end