-
Notifications
You must be signed in to change notification settings - Fork 735
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
Feature/transport prepare request delegate #257
Feature/transport prepare request delegate #257
Conversation
…request preparation delegate
…ses an asynchronous request preparation
@johntmcintosh: 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/ |
The added delegate method for inspecting/preprocessing seems a bit much without a clear use case for the more advanced signature. For response logging, something like this seems sufficient: func networkTransport(_ networkTransport: HTTPNetworkTransport, request: URLRequest, didReceive response: URLResponse, data: Data) Alternatively, maybe it would make sense to expose |
We also need to align this with #223 (comment). You previously mentioned a need for passing in That would allow you to do something like (@knutaa): func networkTransport(_ networkTransport: HTTPNetworkTransport, context: inout Context, request: URLRequest, didReceive response: HTTPURLResponse, data: Data) {
context["requestId"] = response.allHeaderFields["Request-Id"]
} (We actually already pass through a |
@martijnwalraven I think we can defer passing the context parameter. The most needed feature for the client would be
Please let me know if I am missing any case which requires context as mandatory param |
@martijnwalraven yep, I was starting from the more complex version just to be as accommodating as possible, but I'm perfectly fine with us dropping back to a simplified version. What's your thought on exposing error objects that might be returned from URLSession through the delegate vs only triggering the delegate on successful responses? For my use-case, it's convenient to be able to separate out logging of network-specific errors from logging for GraphQL errors (for example, request timed out error from URLSession vs parsing error from GraphQL). If we do support breaking that out, the options are probably either:
or
What do you think about either of those approaches? To avoid this particular PR getting too unweildy, I've been thinking about the context parameter as something that we'd do in a subsequent PR on top of this one, as long as we're comfortable that whatever we start with here lends itself to adding the context afterwards. That approach work for you? |
I agree, I assumed we would still have the So I think the delegate would then look like: public protocol HTTPNetworkTransportDelegate: class {
func networkTransport(_ networkTransport: HTTPNetworkTransport, prepareRequest request: URLRequest, completionHandler: @escaping (URLRequest) -> Void)
func networkTransport(_ networkTransport: HTTPNetworkTransport, request: URLRequest, didReceive response: URLResponse, data: Data)
func networkTransport(_ networkTransport: HTTPNetworkTransport, request: URLRequest, didFailWithError error: Error, retryHandler: @escaping (_ shouldRetry: Bool) -> Void)
} I think it's fine to set aside context for now and break up the PR, but we probably want to make a decision on that before the next release because adding a |
@martijnwalraven, yep, sounds good on the
Thanks! |
@johntmcintosh Sorry, I've been meaning to respond, but got preoccupied with other stuff. I think the code looks great!
|
@AnandWalvekar I don't think you'd want to use a delegate method to read the authentication token for the login response. In my mind, authentication would be a separate non-GraphQL call that wouldn't be handled by |
Thanks @martijnwalraven! Both work and personal life have gotten a bit hectic for me as well. We have a temporary workaround in place in our app right now, but are planning to swap to this implementation once we're all set. I'll do my best to have a solid pass on these ready by the end of the week. |
I'm not quite sure if a transport delegate API would add significant benefit, as right now it's pretty to easy to get the same behaviour with a custom transport. I've just published a package that implements a custom transport with request and response logging (with debug curl CLI output as well): https://github.com/maxdesiatov/ApolloAlamofire |
It is very useful to have the ApolloAlamofire to show that it is quite possible to handle customer specific needs with a custom transport. At the same time, I believe it will be valid to have the transport delegate API as an OOTB capability with minimal dependencies. Looks like the proposal is quite ready to be merged with prepareRequest even if we at this time do not have available code for didReceive and didFailWithError (but would have been good ...) |
Hey all - my apologies for not getting this over the finish line more quickly. I've had a few surprises come up in my personal life recently which have kept me from having the time I expected to do this on the schedule I was originally expecting. I am targeting next week for some dedicated time to get those other delegates in place. |
@johntmcintosh Any idea when these will be done? Thanks! |
Any updates? 🙂 |
When can this be merged? This is a much needed feature for a lot of us. @johntmcintosh thanks! |
Hey @johntmcintosh - 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. |
@martijnwalraven @MrAlek , I'm still in the process of adding the additional delegate's that we've discussed in Slack related to #37, but wanted to go ahead and push up what I have for the first delegate to give you both a chance to give any initial feedback on how things are shaping up.
This PR adds a
HTTPNetworkTransportDelegate
toHTTPNetworkTransport
which has a single function for asynchronous preparation of the request that is about to be sent out. To support this, the transportsend
function creates aURLRequestOperation
which wraps theURLSessionTask
, rather than directly creating the session task inside the transport.Relatedly, I've pushed up an additional commit to my fork that adds a hook for inspecting/preprocessing a network response before it's parsed by Apollo. In my use-case this would be beneficial for allowing us to inject some network logging of the raw requests and responses, but seems like something others would find beneficial as well. I decided to wait on adding that one to this branch so you could take a look at this in isolation first, but you can also view the diff for that commit at this link above.