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

Add router config validate subcommand #6131

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
9 changes: 9 additions & 0 deletions .changesets/feat_feature_configvalidatesubcommand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Add router config validate subcommand ([PR #6131](https://github.com/apollographql/router/pull/6131))

Added new `router config validate` subcommand to allow validation of a Router config file without fully starting up the Router.

```
./router config validate <path-to-config-file.yaml>
```

By [@andrewmcgivery](https://github.com/andrewmcgivery) in https://github.com/apollographql/router/pull/6131
3 changes: 2 additions & 1 deletion apollo-router/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ use self::expansion::Expansion;
pub(crate) use self::experimental::Discussed;
pub(crate) use self::schema::generate_config_schema;
pub(crate) use self::schema::generate_upgrade;
pub(crate) use self::schema::validate_yaml_configuration;
pub(crate) use self::schema::Mode;
use self::subgraph::SubgraphConfiguration;
use crate::cache::DEFAULT_CACHE_CAPACITY;
use crate::configuration::schema::Mode;
use crate::graphql;
use crate::notification::Notify;
use crate::plugin::plugins;
Expand Down
3 changes: 0 additions & 3 deletions apollo-router/src/configuration/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ pub(crate) fn generate_config_schema() -> RootSchema {
#[derive(Eq, PartialEq)]
pub(crate) enum Mode {
Upgrade,

// This is used only in testing to ensure that we don't allow old config in our tests.
#[cfg(test)]
NoUpgrade,
}

Expand Down
26 changes: 26 additions & 0 deletions apollo-router/src/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ use regex::Regex;
use url::ParseError;
use url::Url;

use crate::configuration::expansion::Expansion;
use crate::configuration::generate_config_schema;
use crate::configuration::generate_upgrade;
use crate::configuration::validate_yaml_configuration;
use crate::configuration::Discussed;
use crate::configuration::Mode;
use crate::metrics::meter_provider;
use crate::plugin::plugins;
use crate::plugins::telemetry::reload::init_telemetry;
Expand Down Expand Up @@ -142,6 +145,14 @@ enum ConfigSubcommand {
#[clap(action = ArgAction::SetTrue, long)]
diff: bool,
},

/// Validate existing Router configuration file
Validate {
/// The location of the config to validate.
#[clap(value_parser, env = "APOLLO_ROUTER_CONFIG_PATH")]
config_path: PathBuf,
},

/// List all the available experimental configurations with related GitHub discussion
Experimental,
/// List all the available preview configurations with related GitHub discussion
Expand Down Expand Up @@ -432,6 +443,21 @@ impl Executable {
println!("{}", serde_json::to_string_pretty(&schema)?);
Ok(())
}
Some(Commands::Config(ConfigSubcommandArgs {
command: ConfigSubcommand::Validate { config_path },
})) => {
let config_string = std::fs::read_to_string(config_path)?;
validate_yaml_configuration(
&config_string,
Expansion::default()?,
Mode::NoUpgrade,
)?
.validate()?;
Comment on lines +450 to +455
Copy link
Contributor

Choose a reason for hiding this comment

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

We also have other validation specific to plugins (not schema validation but the content of these configurations)

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean that's ok but we should at least document what it clearly does

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, not understanding your comment 😓

Are you saying that this code does more than just validate the Router config file?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm just saying that on plugins we have other config validations. For example here which validate that the config is logically correct, not only syntactically. So what I would like to have is to document that your new config validate will only validate it's syntactically correct regarding the json schema. That's all

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!


println!("Configuration at path {:?} is valid!", config_path);

Ok(())
}
Some(Commands::Config(ConfigSubcommandArgs {
command: ConfigSubcommand::Upgrade { config_path, diff },
})) => {
Expand Down
16 changes: 16 additions & 0 deletions docs/source/reference/router/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ GraphOS Router and Apollo Router Core provide a set of subcommands for interacti
```
./router config schema
./router config upgrade <path-to-config-file.yaml>
./router config validate <path-to-config-file.yaml>
```

<table class="field-table api-ref">
Expand Down Expand Up @@ -389,6 +390,21 @@ For details, see [Upgrading your router configuration](#upgrading-your-router-co
</td>
</tr>

<tr>
<td>

##### `validate`

</td>
<td>

Takes a config file and validates it against the router's full supported configuration format.

Note: This is a static validation that checks if it is syntactically correct using the JSON schema. The router does additional logical checks on startup against the config that this command does not capture.

</td>
</tr>

</tbody>
</table>

Expand Down
Loading