apollographql / router

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

Additional global variables for Rhai scripts #6041

Open dupham-indeed opened 1 month ago

dupham-indeed commented 1 month ago

Is your feature request related to a problem? Please describe.

Currently, Rhai's support for global variables is limited to only things that are maintained by Router.* (https://www.apollographql.com/docs/router/customizations/rhai/#global-variables) without the option to store additional ones

Describe the solution you'd like

During a Rhai script callback functions' execution, our application would like to create and maintain additional values as global variables that can be later used by other router request lifecycles

fn subgraph_service(service, subgraph){
    let v = Router.EXTRA_VARIABLE;
    let f = |request| {
        print(v);
    };
    service.map_request(f);
    Router.EXTRA_VARIABLE++;
}

Describe alternatives you've considered

A coprocessor can achieve this but we would like to use Rhai to improve performance by minimizing communication costs between the coprocessor and the router

Additional context

Add any other context or screenshots about the feature request here.