Skip to content

Commit

Permalink
server: fix graphiql issue when querying subgraph names with multiple…
Browse files Browse the repository at this point in the history
… path segments
  • Loading branch information
incrypto32 committed Jan 16, 2024
1 parent 6fe6676 commit f016503
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions server/http/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ where
}
}

// Filter out empty strings from path segments
fn filter_and_join_segments(segments: &[&str]) -> String {
segments
.iter()
.filter(|&&segment| !segment.is_empty())
.map(|&segment| segment)
.collect::<Vec<&str>>()
.join("/")
}

let is_mutation = req
.uri()
.query()
Expand All @@ -351,41 +361,35 @@ where
.trim()
.to_lowercase()
.starts_with("mutation");

match (method, path_segments.as_slice()) {
(Method::GET, [""]) => self.index().boxed(),
(Method::GET, &["subgraphs", "id", _, "graphql"])
| (Method::GET, &["subgraphs", "name", _, "graphql"])
| (Method::GET, &["subgraphs", "name", _, _, "graphql"])
| (Method::GET, &["subgraphs", "name", .., "graphql"])
| (Method::GET, &["subgraphs", "network", _, _, "graphql"])
| (Method::GET, &["subgraphs", "graphql"]) => self.handle_graphiql(),

(Method::GET, _path @ ["subgraphs", "name", _, _]) if is_mutation => {
(Method::GET, _path @ ["subgraphs", "name", ..]) if is_mutation => {
self.handle_mutations()
}
(Method::GET, path @ ["subgraphs", "id", _])
| (Method::GET, path @ ["subgraphs", "name", _])
| (Method::GET, path @ ["subgraphs", "name", _, _])
| (Method::GET, path @ ["subgraphs", "name", ..])
| (Method::GET, path @ ["subgraphs", "network", _, _]) => {
let dest = format!("/{}/graphql", path.join("/"));
let filtered_path = filter_and_join_segments(path);
let dest = format!("/{}/graphql", filtered_path);
self.handle_temp_redirect(dest).boxed()
}

(Method::POST, &["subgraphs", "id", subgraph_id]) => {
self.handle_graphql_query_by_id(subgraph_id.to_owned(), req)
}
(Method::OPTIONS, ["subgraphs", "id", _]) => self.handle_graphql_options(req),
(Method::POST, &["subgraphs", "name", subgraph_name]) => self
.handle_graphql_query_by_name(subgraph_name.to_owned(), req)
.boxed(),
(Method::POST, ["subgraphs", "name", ..]) => {
let subgraph_name = path_segments[2..].join("/");
(Method::POST, path @ ["subgraphs", "name", ..]) => {
let subgraph_name = filter_and_join_segments(&path[2..]);
self.handle_graphql_query_by_name(subgraph_name, req)
.boxed()
}

(Method::OPTIONS, ["subgraphs", "name", _])
| (Method::OPTIONS, ["subgraphs", "name", _, _]) => self.handle_graphql_options(req),
(Method::OPTIONS, ["subgraphs", "name", ..]) => self.handle_graphql_options(req),

_ => self.handle_not_found(),
}
Expand Down

0 comments on commit f016503

Please sign in to comment.