graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Proposal: Make headers available after request

Open softwaregravy opened this issue 4 years ago • 2 comments

My use case is that while troubleshooting with Shopify, they need a header from the request to track find the call on their side. Needing to get values from the header of requests to APIs seems generally useful to me, and I suggest making it a part of the library.

Currently, the response object is a local variable inside the GraphQL::Client::HTTP#execute method. I could find no clean way to get the headers from the response.

I propose keeping the response headers stored on the class where they can be retrieved.

Running locally, I have verified that this allows me to do the following:

client = ShopifyAPI::GraphQL.client
query = client.parse(...)
ShopifyAPI::GraphQL.client.query(query)
client.execute.response_headers["x-request-id"]

The response_headers method could also be delegated from the client, which would allow us to write client.response_headers["x-request-id"].

I don't yet have the tests passing locally, but I wanted to get some feedback before spending more cycles on this.

Thoughts?

softwaregravy avatar May 21 '20 15:05 softwaregravy

Having this would be great.

I just had a similar need and if someone else comes here looking for an answer, here's what you can do for a quick bypass: You can pass a custom execute method when instantiating an HTTP adapter just like the readme does with the headers method in the Configuration section. This custom method can be just a copy of the original method but any key that you set a value to in the returned hash will be available in the original_hash attribute of the query reponse object.

GabrielErbetta avatar Aug 12 '20 02:08 GabrielErbetta

I am also struggling with this issue and I think we should work a path forward for it.

That being said while this implementation allow retrieval, I see it problematic as the client is typically being re-used and what you call response_headers is really "last response header".

Do we prefer this over something more traditional:

client = ShopifyAPI::GraphQL.client
query = client.parse(...)
response = ShopifyAPI::GraphQL.client.query(query)

response.data
response.headers # Headers now being exposed

tjoyal avatar Sep 15 '20 11:09 tjoyal