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
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
  • Loading branch information
billybonks committed Nov 12, 2024
1 parent 75fce88 commit 1d42e19
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 1d42e19

Please sign in to comment.