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

server: fix graphiql issue when querying subgraph names with multiple path segments #5136

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading