Skip to content

Commit

Permalink
feat: Enable consumers to access the full response payload
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
billybonks committed Nov 10, 2024
1 parent 75fce88 commit 202b5be
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions lib/graphql/client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion lib/graphql/client/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 202b5be

Please sign in to comment.