Skip to content

Commit

Permalink
Merge pull request #43 from billybonks/feat/access-full-response
Browse files Browse the repository at this point in the history
feat: Enable consumers to access the full response payload
  • Loading branch information
rmosolgo authored Nov 13, 2024
2 parents 75fce88 + ca63d53 commit b547fa4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,12 @@ 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: extensions,
full_response: execute.respond_to?("last_response") ? execute.last_response : nil
)
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 b547fa4

Please sign in to comment.