Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access a response header in the FetchResponse (client side) #12265

Open
shaundclements opened this issue Jan 13, 2025 · 4 comments
Open
Labels

Comments

@shaundclements
Copy link

Can anyone advise how I can retrieve a response header in my client (React Code) - I am using the useQuery hook but it seems rather limited.

Ive also tried modifying the response data returned back to the client (see 'Modifying response data' from https://www.apollographql.com/docs/react/networking/advanced-http-networking) with an extra field returned back but it seems to be readonly.

@phryneas
Copy link
Member

The problem here is that Apollo Client is a GraphQL client and doesn't really know about HTTP concepts outside of the Link chain - the query could be made any number of different ways, http only being one of them.

Your second attempt (adding it to the response data) could work, but would need quite a lot more work:

  • use a document transform to add a new field to the outgoing query (this way the cache is aware of that field and will parse it off the response)
  • in the link chain, remove that field for outgoing requests so the server doesn't see it
  • in the link chain, add the field value back for incoming responses

@shaundclements
Copy link
Author

shaundclements commented Jan 13, 2025

going to try your suggestion thanks, I am surprised there is no access to the response headers given the client is making an http request

@shaundclements
Copy link
Author

shaundclements commented Jan 14, 2025

@phryneas for your first two points, is that done in the query definition 'SelectionSet' and 'loc' ?
For your last point, is that just response.data area ?

@phryneas
Copy link
Member

phryneas commented Jan 14, 2025

I am surprised there is no access to the response headers given the client is making an http request

As I said, the Client is agnostic to the transport method you are using, which is why it's not aware of this. You could also e.g. use WebSockets for this - the Client is a pure GraphQL client, not a HTTP Client.

for your first two points, is that done in the query definition 'SelectionSet' and 'loc' ?

Yes! As examples you can see the addTypeNameToDocument transform and the other transforms in the same file.

For your last point, is that just response.data area ?

Yes, your data shape would have to match up with the shape of the query you pass into the link chain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants