-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Batched network interface runs middleware and afterware multiple times #1191
Comments
@sheerun Good point, I think you're right about running only once, even on batched queries. I do think that we'll still need a kind of middleware and afterware that runs for each individual query, and the names Would be great if you (or someone else) could make a PR. Don't be afraid to ping me directly if you need any pointers/hints. 🙂 |
Having thought about this a little bit more, I don't think middleware and afterware is currently used to alter the body of the GraphQL request (nor is it recommended), so an initial fix for this should simply be to change the behavior of batched network interface to run on the batched query. If it turns out that there's a good use-case for queryMiddleware and queryAfterware, we can always add those later. |
Please consider about
|
@helfer I'll take a stab at doing the fix you suggested. |
@lewisf Great, I look forward to it! 🙂 |
@lewisf Any progress? Let me know if I can assist with anything |
Hey @sondremare, got busy with other things. I have test cases, and a work in progress branch that I'll make a PR with in the upcoming days. |
This is fixed now, thanks to @lewisf ! ❤️ |
@helfer @lewisf Looks good. It's just shame you didn't change behavior of applyMiddleware to what batchMiddleware is implemented here. queryMiddleware is both more generic name and would allow for better uniformity and backward compatibility of passing request / response to applyMiddleware
These generic api methods can be implemented by network interfaces. On the other hand |
Interesting - I never really considered that sharing middlewares between different network interfaces would be that useful. Since different interfaces have different transport mechanisms, seems like it would be hard. Of course some stuff like the query response data will be the same across transports, but that seems like the minority. |
Introduction
Here's how you introduce middleware in your docs:
"In both examples, we’ll show how you would add an authentication token to the HTTP header of the requests being sent by the client."
To support this you show simple afterware that introspects responses's code:
To me message is simple: middleware and afterware allow you to change request and introspect response to/from the graphql backend. You also properly recognize that middleware is mostly useful for authenticating request, and afterware is mostly useful for introspecting responses, e.g. to logout when status is 401.
Additionally you have concept of networkInterfaces that (I suppose) ideally could be just drop-in replaced. e.g. I could exchange NetworkInteface with BatchedNetworkInterface without changing any code.
Issue
Unfortunately both of these use cases fail with current implementation of batched network interface. Namely is runs middleware and afterware for each of batched queries instead all of them in whole. It also breaks the interface of applyAfterware, because e.g.
response.status
is unavailable.The logic for network interface assumes, middleware and afterware runs just one time, middleware adding authentication headers, and afterware checking for response status after receiving it from server. If we exchanged network interface for batched network interface for examples above, we:
logout
is dispatched multiple timesrequest.status
is not available as payload..Summary
I hope I've shown you that the decision to run middleware and afterware for each batched query and response for batched network interface is wrong.
How you could fix it? Run middleware and afterware just one time, on request object after the batching is performed. The interface and contract for both should be 100% the same as on network interface.
If you want a possibility to middleware individual queries within batch, you can introduce extra
queryMiddleware
andqueryAfterware
that work of individual queries, not on whole requests.Thank you, you're awesome
The text was updated successfully, but these errors were encountered: