Skip to content

Commit 6789557

Browse files
committed
server: fix graphiql issue when querying subgraph names with multiple path segments
1 parent 747b8da commit 6789557

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

server/http/src/service.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ where
339339
}
340340
}
341341

342+
// Filter out empty strings from path segments
343+
fn filter_and_join_segments(segments: &[&str]) -> String {
344+
segments
345+
.iter()
346+
.filter(|&&segment| !segment.is_empty())
347+
.map(|&segment| segment)
348+
.collect::<Vec<&str>>()
349+
.join("/")
350+
}
351+
342352
let is_mutation = req
343353
.uri()
344354
.query()
@@ -351,41 +361,35 @@ where
351361
.trim()
352362
.to_lowercase()
353363
.starts_with("mutation");
354-
355364
match (method, path_segments.as_slice()) {
356365
(Method::GET, [""]) => self.index().boxed(),
357366
(Method::GET, &["subgraphs", "id", _, "graphql"])
358-
| (Method::GET, &["subgraphs", "name", _, "graphql"])
359-
| (Method::GET, &["subgraphs", "name", _, _, "graphql"])
367+
| (Method::GET, &["subgraphs", "name", .., "graphql"])
360368
| (Method::GET, &["subgraphs", "network", _, _, "graphql"])
361369
| (Method::GET, &["subgraphs", "graphql"]) => self.handle_graphiql(),
362370

363-
(Method::GET, _path @ ["subgraphs", "name", _, _]) if is_mutation => {
371+
(Method::GET, _path @ ["subgraphs", "name", ..]) if is_mutation => {
364372
self.handle_mutations()
365373
}
366374
(Method::GET, path @ ["subgraphs", "id", _])
367-
| (Method::GET, path @ ["subgraphs", "name", _])
368-
| (Method::GET, path @ ["subgraphs", "name", _, _])
375+
| (Method::GET, path @ ["subgraphs", "name", ..])
369376
| (Method::GET, path @ ["subgraphs", "network", _, _]) => {
370-
let dest = format!("/{}/graphql", path.join("/"));
377+
let filtered_path = filter_and_join_segments(path);
378+
let dest = format!("/{}/graphql", filtered_path);
371379
self.handle_temp_redirect(dest).boxed()
372380
}
373381

374382
(Method::POST, &["subgraphs", "id", subgraph_id]) => {
375383
self.handle_graphql_query_by_id(subgraph_id.to_owned(), req)
376384
}
377385
(Method::OPTIONS, ["subgraphs", "id", _]) => self.handle_graphql_options(req),
378-
(Method::POST, &["subgraphs", "name", subgraph_name]) => self
379-
.handle_graphql_query_by_name(subgraph_name.to_owned(), req)
380-
.boxed(),
381-
(Method::POST, ["subgraphs", "name", ..]) => {
382-
let subgraph_name = path_segments[2..].join("/");
386+
(Method::POST, path @ ["subgraphs", "name", ..]) => {
387+
let subgraph_name = filter_and_join_segments(&path[2..]);
383388
self.handle_graphql_query_by_name(subgraph_name, req)
384389
.boxed()
385390
}
386391

387-
(Method::OPTIONS, ["subgraphs", "name", _])
388-
| (Method::OPTIONS, ["subgraphs", "name", _, _]) => self.handle_graphql_options(req),
392+
(Method::OPTIONS, ["subgraphs", "name", ..]) => self.handle_graphql_options(req),
389393

390394
_ => self.handle_not_found(),
391395
}

0 commit comments

Comments
 (0)