Skip to content
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

Support multiple gateways in the same process #261

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions gateway-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { getVariableValues } from 'graphql/execution/values';
import fetcher from 'make-fetch-happen';
import { HttpRequestCache } from './cache';
import { fetch } from 'apollo-server-env';
import { getQueryPlanner } from '@apollo/query-planner-wasm';
import { getQueryPlanner, updatePlannerSchema } from '@apollo/query-planner-wasm';

export type ServiceEndpointDefinition = Pick<ServiceDefinition, 'name' | 'url'>;

Expand Down Expand Up @@ -417,7 +417,11 @@ export class ApolloGateway implements GraphQLService {
)
} else {
this.schema = schema;
this.queryPlannerPointer = getQueryPlanner(composedSdl);
if (this.queryPlannerPointer != null) {
updatePlannerSchema(this.queryPlannerPointer, composedSdl)
} else {
this.queryPlannerPointer = getQueryPlanner(composedSdl);
}

// Notify the schema listeners of the updated schema
try {
Expand Down
54 changes: 43 additions & 11 deletions query-planner-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,30 @@ static mut DATA: Vec<QueryPlanner> = vec![];
#[wasm_bindgen(js_name = getQueryPlanner)]
pub fn get_query_planner(schema: JsString) -> usize {
unsafe {
if SCHEMA.is_empty() {
SCHEMA.push(String::from(schema));
DATA.push(QueryPlanner::new(&SCHEMA[0]));
} else {
SCHEMA[0] = String::from(schema);
DATA[0] = QueryPlanner::new(&SCHEMA[0]);
let idx = SCHEMA.len();
SCHEMA.push(String::from(schema));
DATA.push(QueryPlanner::new(&SCHEMA[idx]));
idx
}
}

#[wasm_bindgen(js_name = updatePlannerSchema)]
pub fn update_planner_schema(idx: usize, schema: JsString) {
unsafe {
if SCHEMA.get(idx).is_none() {
panic!("Index {} not found", idx)
}
let data = &DATA[0];
data as *const QueryPlanner as usize

SCHEMA[idx] = String::from(schema);
DATA[idx] = QueryPlanner::new(&SCHEMA[idx]);
}
}

#[wasm_bindgen(js_name = getQueryPlan)]
pub fn get_query_plan(planner_ptr: usize, query: &str, options: &JsValue) -> JsValue {
pub fn get_query_plan(idx: usize, query: &str, options: &JsValue) -> JsValue {
let options: QueryPlanningOptions = options.into_serde().unwrap();
unsafe {
let planner = planner_ptr as *const QueryPlanner;
let planner: &QueryPlanner = &*planner;
let planner = &DATA[idx];
let plan = planner.plan(query, options).unwrap();
JsValue::from_serde(&plan).unwrap()
}
Expand Down Expand Up @@ -64,4 +70,30 @@ mod tests {
let plan = result.into_serde::<QueryPlan>().unwrap();
assert_eq!(plan, expected);
}

#[wasm_bindgen_test]
fn multiple_query_planners() {
let schema_multiple_keys = include_str!(
"../../stargate/crates/query-planner/tests/features/multiple-keys/csdl.graphql"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason we're stringly typing paths

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this seems to pass on windows but i am surprised)

);
let planner_multiple_keys = get_query_planner(JsString::from(schema_multiple_keys));
let query_multiple_keys = "query { reviews { body } }";

let schema_basic =
include_str!("../../stargate/crates/query-planner/tests/features/basic/csdl.graphql");
let planner_basic = get_query_planner(JsString::from(schema_basic));
let query_basic = "query { me { name } }";

let options = QueryPlanningOptionsBuilder::default().build().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should at least make these unwraps expects.. if these were to fail it would be very unpleasant to debug

let options = JsValue::from_serde(&options).unwrap();

let result_basic = get_query_plan(planner_basic, query_basic, &options);
let plan_basic = result_basic.into_serde::<QueryPlan>().unwrap();

let result_multiple_keys =
get_query_plan(planner_multiple_keys, query_multiple_keys, &options);
let plan_multiple_keys = result_multiple_keys.into_serde::<QueryPlan>().unwrap();

assert_ne!(plan_multiple_keys, plan_basic);
}
}