apollographql / router

A configurable, high-performance routing runtime for Apollo Federation 🚀
https://www.apollographql.com/docs/router/
Other
795 stars 266 forks source link

Import falls out of scope when passing a curried function to `service.map_request` #4355

Closed andrewmcgivery closed 8 months ago

andrewmcgivery commented 9 months ago

Describe the bug We have a client trying to access the name of the subgraph within a rhai script from a map_request function. As far as I can tell, the name is available in the subgraph_service function but it is not exposed via request.subgraph once map_request is called. So, he is trying to do a call with curry to inject it.

However, when he does this, an imported module is no longer in scope that should be.

To Reproduce The following code reproduces the issue:

import "subgraph_module" as subgraph_module;

fn request_callback(subgraph, request) {
  subgraph_module::process_request(subgraph, request); // throws error: "Module not found: subgraph_module" 
}

fn subgraph_service(service, subgraph) {
  let cb = Fn("request_callback");
  cb = curry(request_callback, subgraph);

  service.map_request(cb);
}

The (hacky) workaround that does seem to work:

fn request_callback(subgraph, request) {
  import "subgraph_module" as subgraph_module;
  subgraph_module::process_request(subgraph, request); // works!
}

fn subgraph_service(service, subgraph) {
  let cb = Fn("request_callback");
  cb = curry(request_callback, subgraph); // curry subgraph to request_callback, since service.map_request only accepts 1 param - "request"

  service.map_request(cb);
}

In the broken example, subgraph_module should be in scope... but it isn't for some reason. In the second example, we are moving the import into the function and it does work.

Expected behavior Imported modules should be in scope.

garypen commented 9 months ago

Unfortunately, I'm fairly sure that this is the expected behaviour in Rhai, as documented here: https://rhai.rs/book/ref/fn-ptr.html

I may be mis-interpreting the documentation, so I'll follow up with the Rhai community and will update this ticket when I have a response.

garypen commented 9 months ago

In conversation with the lead Rhai developer, this definitely is expected behaviour and there's no downside to importing the module in the function directly as the workaround suggests.

This is essentially a limitation in Rhai itself and so I think we should close this issue here.

Geal commented 8 months ago

closing this issue for now, feel free to reopen if there's new approachs available in rhai that could help here