graphql.js icon indicating copy to clipboard operation
graphql.js copied to clipboard

Cannot get response headers

Open soundstep opened this issue 3 years ago • 1 comments

Hi,

I was thinking about client-side request caching to optimize some CLI requests, I would probably need to get the request response headers for that.

Unless I missed something, it looks like there is no way to retrieve the response headers once the Github request.

Is that something that could be done?

Maybe with an option?

Without extended option (as it is currently)

graphql.defaults({
    request: {
        extendedResponse: false
    }
})
const data = await graphql(query);

With extended option (what I would need)

graphql.defaults({
    request: {
        extendedResponse: true
    }
})
const response = await graphql(query);
const { data, request } = response;
console.log(request.status); // HTTP status
console.log(request.headers); // response headers
console.log(data); // actual response (same as request.data)

Any thoughts?

soundstep avatar Nov 09 '21 10:11 soundstep

Hi @soundstep,

The @octokit/request module is used under the hood and should provide access to headers.

Unfortunately, I haven't used this module, and haven't touched it's codebase, so I'm unable to help.

Please note that @octokit has no current maintainer.

You can check out and subscribe to https://github.com/octokit/octokit.js/discussions/620#discussioncomment-1472174 for updates

wolfy1339 avatar Nov 10 '21 01:11 wolfy1339

I did a quick dive into the code for this, and it seems like without modifying the existing code, response headers are being left off:

https://github.com/octokit/graphql.js/blob/b54bea050351d1363304176f296451cae3c6011f/src/graphql.ts#L86

Not sure about it as the final implementation, but riffing off your extendedResponse idea @soundstep; I think something like the following will work:

// ...
+    if (requestOptions.extendedResponse) {
+       const extendedResponse = response.data.data;
+       extendedResponse.headers = response.headers;
+       return extendedResponse;
+    }
    return response.data.data;
// ...

virtuoushub avatar Nov 26 '22 18:11 virtuoushub

Do you happen to have any updates on this feature request?

Best practices according to the GitHub GraphQL API Docs is to use the rate limit response headers instead of querying the API to check your rate limit.

eliasbrange avatar Nov 14 '23 06:11 eliasbrange

Use https://github.com/octokit/plugin-throttling.js/ for handling rate limits, we recommend you use https://github.com/octokit/octokit.js which has all recommended best practices built in.

We do not plan to expose headers through @octokit/graphql. Use @octokit/request instead as suggested.

gr2m avatar Nov 14 '23 17:11 gr2m