From 202b5be423302c1d1cbb6c79c388dea385fdfd16 Mon Sep 17 00:00:00 2001 From: billybonks Date: Sun, 10 Nov 2024 15:24:35 +0800 Subject: [PATCH] feat: Enable consumers to access the full response payload create end user public api in the response object called full_response, this is non breaking and additive feature that should not need to change. Propergating it from internal class with @last_response attribute, we can change this to be in the return object of execute, but i would need to know all the places that i need to change. --- lib/graphql/client.rb | 3 ++- lib/graphql/client/http.rb | 6 ++++++ lib/graphql/client/response.rb | 8 +++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/graphql/client.rb b/lib/graphql/client.rb index f064ca2e..98bc9a48 100644 --- a/lib/graphql/client.rb +++ b/lib/graphql/client.rb @@ -385,7 +385,8 @@ def query(definition, variables: {}, context: {}) result, data: definition.new(data, Errors.new(errors, ["data"])), errors: Errors.new(errors), - extensions: extensions + extensions:, + full_response: execute.last_response ) 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