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

fix: add a consistent way to use endpoint path #2410

Merged
merged 7 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ By [@osamra-rbi](https://github.com/osamra-rbi) in https://github.com/apollograp

## 🐛 Fixes

### Better support for wildcard in `supergraph.path` configuration ([Issue #2406](https://github.com/apollographql/router/issues/2406))

You can now use wildcard in supergraph endpoint path like this:

```yaml
supergraph:
listen: 0.0.0.0:4000
path: /g*
```

if you want the supergraph to answer on `/graphql` or `/gateway` for example.

By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/2410

### Fix panic in schema parse error reporting ([Issue #2269](https://github.com/apollographql/router/issues/2269))

In order to support introspection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,8 @@ pub(super) fn main_router<RF>(configuration: &Configuration) -> axum::Router
where
RF: RouterFactory,
{
let mut graphql_configuration = configuration.supergraph.clone();
if graphql_configuration.path.ends_with("/*") {
// Needed for axum (check the axum docs for more information about wildcards https://docs.rs/axum/latest/axum/struct.Router.html#wildcards)
graphql_configuration.path = format!("{}router_extra_path", graphql_configuration.path);
}

Router::new().route(
&graphql_configuration.path,
&configuration.supergraph.sanitized_path(),
get({
move |Extension(service): Extension<RF>, request: Request<Body>| {
handle_graphql(service.create().boxed(), request)
Expand Down
29 changes: 28 additions & 1 deletion apollo-router/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use std::str::FromStr;
use derivative::Derivative;
use displaydoc::Display;
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use schemars::gen::SchemaGenerator;
use schemars::schema::ObjectValidation;
use schemars::schema::Schema;
Expand All @@ -41,6 +43,11 @@ use crate::configuration::schema::Mode;
use crate::executable::APOLLO_ROUTER_DEV_ENV;
use crate::plugin::plugins;

static SUPERGRAPH_ENDPOINT_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(?P<first_path>.*/)(?P<sub_path>.+)\*$")
.expect("this regex to check the path is valid")
});

/// Configuration error.
#[derive(Debug, Error, Display)]
#[non_exhaustive]
Expand Down Expand Up @@ -387,7 +394,10 @@ impl Configuration {
),
});
}
if self.supergraph.path.ends_with('*') && !self.supergraph.path.ends_with("/*") {
if self.supergraph.path.ends_with('*')
&& !self.supergraph.path.ends_with("/*")
&& !SUPERGRAPH_ENDPOINT_REGEX.is_match(&self.supergraph.path)
{
return Err(ConfigurationError::InvalidConfiguration {
message: "invalid 'server.graphql_path' configuration",
error: format!(
Expand Down Expand Up @@ -583,6 +593,23 @@ impl Default for Supergraph {
}
}

impl Supergraph {
/// To sanitize the path for axum router
pub(crate) fn sanitized_path(&self) -> String {
let mut path = self.path.clone();
if self.path.ends_with("/*") {
// Needed for axum (check the axum docs for more information about wildcards https://docs.rs/axum/latest/axum/struct.Router.html#wildcards)
path = format!("{}router_extra_path", self.path);
} else if SUPERGRAPH_ENDPOINT_REGEX.is_match(&self.path) {
let new_path = SUPERGRAPH_ENDPOINT_REGEX
.replace(&self.path, "${first_path}${sub_path}:supergraph_route");
path = new_path.to_string();
}

path
}
}

/// Automatic Persisted Queries (APQ) configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
Expand Down
53 changes: 43 additions & 10 deletions apollo-router/src/configuration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,6 @@ fn empty_config() {
.expect("should have been ok with an empty config");
}

#[test]
fn bad_graphql_path_configuration_with_bad_ending_wildcard() {
let error = Configuration::fake_builder()
.supergraph(Supergraph::fake_builder().path("/test*").build())
.build()
.unwrap_err();

assert_eq!(error.to_string(), String::from("invalid 'server.graphql_path' configuration: '/test*' is invalid, you can only set a wildcard after a '/'"));
}

#[test]
fn line_precise_config_errors() {
let error = validate_yaml_configuration(
Expand Down Expand Up @@ -683,3 +673,46 @@ fn visit_schema(path: &str, schema: &Value, errors: &mut Vec<String>) {
_ => {}
}
}

#[test]
fn test_configuration_validate_and_sanitize() {
let conf = Configuration::builder()
.supergraph(Supergraph::builder().path("/g*").build())
.build()
.unwrap()
.validate()
.unwrap();
assert_eq!(&conf.supergraph.sanitized_path(), "/g:supergraph_route");

let conf = Configuration::builder()
.supergraph(Supergraph::builder().path("/graphql/g*").build())
.build()
.unwrap()
.validate()
.unwrap();
assert_eq!(
&conf.supergraph.sanitized_path(),
"/graphql/g:supergraph_route"
);

let conf = Configuration::builder()
.supergraph(Supergraph::builder().path("/*").build())
.build()
.unwrap()
.validate()
.unwrap();
assert_eq!(&conf.supergraph.sanitized_path(), "/*router_extra_path");

let conf = Configuration::builder()
.supergraph(Supergraph::builder().path("/test").build())
.build()
.unwrap()
.validate()
.unwrap();
assert_eq!(&conf.supergraph.sanitized_path(), "/test");

assert!(Configuration::builder()
.supergraph(Supergraph::builder().path("/*/whatever").build())
.build()
.is_err());
}
1 change: 1 addition & 0 deletions docs/source/configuration/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ Path parameters and wildcards are supported. For example:

- `/:my_dynamic_prefix/graphql` matches both `/my_project_a/graphql` and `/my_project_b/graphql`.
- `/graphql/*` matches `/graphql/my_project_a` and `/graphql/my_project_b`.
- `/g*` matches `/graphql` and `/gateway`.

> **Note:** The router does _not_ support wildcards in the _middle_ of a path (e.g., `/*/graphql`). Instead, use a path parameter (e.g., `/:parameter/graphql`).

Expand Down