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

Add httpResponse property to GraphQLResponse object in order to get access to its headers #223

Closed
wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented Mar 12, 2018

No description provided.

@apollo-cla
Copy link

@TomaszWlodarczyk: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Meteor Contributor Agreement here: https://contribute.meteor.com/

@ghost ghost changed the title Add httpResponse property to GraphQLResponse class in order to get access to its headers Add httpResponse property to GraphQLResponse object in order to get access to its headers Mar 12, 2018
@martijnwalraven
Copy link
Contributor

Thanks for submitting a PR!

I don't think we can expose the HTTP response on the GraphQLResponse directly, because that ties it to a specific transport. There is ongoing work to add support for the WebSocket transport for example, and that wouldn't work with this.

Maybe we can add a more generic context to GraphQLResponse that transports can add data to? What is your use case exactly? What headers would you like access to?

@ghost
Copy link
Author

ghost commented Mar 12, 2018

The thing is that we pass over from backend to the app a unique response ID as a custom HTTP header in order to trace it later in case of any issues. That's why I need to have an access to that. Is there any other way to do so?

But you do that in case of response failure anyway....

Anyway adding all headers from HTTPResponse to GraphQLResponse would solve may case

@martijnwalraven
Copy link
Contributor

@knutaa @MrAlek are working on the WebSocket transport and changes to the networking layer, so they may have more specific ideas. A simple solution might be to add a context map to GraphQLResponse, and have HTTPNetworkTransport set headerFieldson it.

@knutaa
Copy link
Contributor

knutaa commented Mar 13, 2018

Regarding generalization of network transport, one way of handling this could be to have the response as an enum (of transports) with associated data. This new enum could then be associated with the server value of the GraphQLResult.Source to be available to the client.

@ghost
Copy link
Author

ghost commented Mar 14, 2018

Is there any chance that you address that in near future?

@MrAlek
Copy link
Contributor

MrAlek commented Mar 16, 2018

I also agree that HTTP headers should be specific to the HTTPNetworkTransport because it's not really part of the GraphQL operation itself (nor the GraphQLResponse).

Another alternative would to be to have a delegate protocol on HTTPNetworkTransport with a callback like
didReceiveResponse(_ response: HTTPURLResponse, for operation: GraphQLOperation) where you would extract headers if you need them. Would that work in your case?

@ghost
Copy link
Author

ghost commented Mar 19, 2018

@MrAlek yeah that would work!

@knutaa
Copy link
Contributor

knutaa commented Mar 20, 2018

To avoid having the client coordinate between two async callbacks (headers and the normal response) and to support multiple transports, the following could also be considered.

The main idea is to have the enum Source take associated data (new TransportMessage) for the server value and the TransportMessage is itself an enum.

See attachment.
ios - apollo - transportmessage.pdf

@ghost
Copy link
Author

ghost commented Apr 11, 2018

Any ETA for introducing that?

@knutaa
Copy link
Contributor

knutaa commented Apr 11, 2018

I have added the proposal based on GraphQLResult.Source to https://github.com/knutaa/apollo-ios. Should be possible to test based on that branch.

But believe some agreement is needed on design before this is progressed

@martijnwalraven
Copy link
Contributor

Hmmm, I don't really like the idea of coupling the Source enum to particular transports. It also puts the burden on callers to interpret these for every result. What about using a delegate method to extract transport-specific information? Something like:

type Context = [String: Any]

func networkTransport(_ networkTransport: HTTPNetworkTransport, contextFor response: HTTPURLResponse): Context

And then we could pass through the returned context to GraphQLResponse and GraphQLResult.

@sfla
Copy link

sfla commented Apr 26, 2018

I've just recently started using Apollo, so I don't have any super smart suggestions. At first glance, I'd think the most natural place to put a HTTPResponse is in a subclass of GraphQLResult named GraphQLHTTPResult which simply includes the response. From my point of view, it would go along nicely with the potential error-object GraphQLHTTPResponseError that can be returned (which does have the HTTPURLResponse). Each object that can be accessed in the fetch's resultHandler (when using a HTTPNetworkTransport) should expose the actual response in top level, shouldn't it?..
Then again, I probably have no idea what I'm talking about.

Anyway, in my case I deal with data for real-time public transportation, converting departure times to "departs in X minutes", and it is very important to be able to access the server's Date-header to prevent misinformation when users have set their device's clock a few minutes back or forth.

@knutaa
Copy link
Contributor

knutaa commented Apr 28, 2018

I have created a new PR #266 based on the suggested use of context. But without use of the suggested delegate. Not sure if that is adding required flexibility as this time.

@designatednerd
Copy link
Contributor

Hey deleted account friend - Thank you for doing this, and sorry for the long radio silence on our end!

Unfortunately since so much has changed since this PR was opened, I'm going to close this in favor of the work happening in #602 to implement this feature. Please feel free to chime in there if you have thoughts or questions, and open issues on anything you feel we may be leaving out.

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

Successfully merging this pull request may close these issues.

7 participants