-
Notifications
You must be signed in to change notification settings - Fork 276
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
implement map_response for deferred responses in rhai #1501
Conversation
we need a specific function to map over deferred responses, so we can make the distinction with the first one, where we have access to headers
This comment has been minimized.
This comment has been minimized.
2e38ba3
to
9897190
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great and it's nice to see the rhai binding catching up with the defer enhancements.
I have some questions about how to use this. It seems like an implementation of RouterService
or ExecutionService
has to decide to either map_response()
or map_deferred_response()
. I think it would be nice to actually "hide" this decision from rhai scripters if possible, so that map_deferred_response
is implicitly chosen for Router and Execution services that call map_response
and not possible to be chosen for QueryPlanning and Subgraph Services.
(I'm aware we can't force this kind of decision on rust plugin users, so we have to expose the complexity there, but it would be really nice to hide this from rhai scripters.)
Once this is resolved, we need to think about how best to document this new capability in the router doc pages for rhai.
it would be possible to do everything in the same response, but then we need to address the issue of header edition: headers can only be modified when looking at the first response, not deferred ones (otherwise we would have to buffer the entire http response). Another thing I worry about here is how to link deferred responses. It looks like Ideally I'd like an API like this: fn router_service(service) {
let f = |response| {
let start = apollo_start.elapsed;
let duration = apollo_start.elapsed - start;
print(`response processing took: ${duration}`);
print(response.body.errors);
let g = |response| {
let start = apollo_start.elapsed;
let duration = apollo_start.elapsed - start;
print(`deferred response processing took: ${duration}`);
print(response.body.errors);
};
response.map_deferred(g);
};
service.map_response(f); But I am not sure it is achievable |
ok, now it is a bit nicer with the same method used everywhere. Accessing the headers from a deferred response will throw: fn supergraph_service(service) {
let f = |response| {
let start = apollo_start.elapsed;
let duration = apollo_start.elapsed - start;
print(`response processing took: ${duration}`);
try {
print(`headers: ${response.headers}`);
} catch(err) {
print(`cannot access headers from a deferred response`)
}
print(response.body.errors);
};
service.map_response(f);
} should I add a |
Talked about this with @SimonSapin during our API:1.0 review and he raised the point that it will be difficult for a programmer to deal with runtime errors due to handling of deferred responses. It's difficult to know what the "best" solution is here, because it's clearly a compromise whatever we do. However, your
I think it's more idiomatic rhai to do what your example does and catch the exception rather than have the I think with documentation and judicious use of |
ok so the |
I think the name is clear enough already since it's a method on a I still think it would be better to not have this function and just handle the exception like you did in your original example. @SimonSapin could you look at the examples and offer up an opinion? BTW: I imagine for your example that won't work right now since we haven't added support for accessing headers on a
and the same again for router::DeferredResponse. And the same pairing for setting of headers... |
The example works because it throws due to a missing function instead of an explicit error |
this will be a better UX than calling an absent method
Fix #1219
Fix #1409
we need a specific function to map over deferred responses, so we can
make the distinction with the first one, where we have access to headers