Skip to content

Commit 3eb81fa

Browse files
fix: make supergraph.{path,key,endpoint} optional (#593)
This commit adds validation to ensure that the supergraph file path, Hive CDN endpoint, and key are provided either through environment variables or the configuration file. It also updates the documentation and configuration structs to reflect that the path and endpoint/key are now optional. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent c490634 commit 3eb81fa

File tree

5 files changed

+46
-18
lines changed

5 files changed

+46
-18
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
router: patch
3+
config: patch
4+
---
5+
6+
# Improve error messages and fix environment variable support for supergraph configuration
7+
8+
- **Fix:** Previously, `supergraph.path` (for file source), and `supergraph.endpoint`/`supergraph.key` (for Hive CDN source) were mandatory in the configuration file. This prevented users from relying solely on environment variables (`SUPERGRAPH_FILE_PATH`, `HIVE_CDN_ENDPOINT`, `HIVE_CDN_KEY`). This has been fixed, and these fields are now optional in the configuration file if the corresponding environment variables are provided.
9+
- **Improved Error Reporting:** If the supergraph file path or Hive CDN endpoint/key are missing from both configuration and environment variables, the error message now explicitly guides you to set the required environment variable or the corresponding configuration option.
10+
11+
This change ensures that misconfigurations are easier to diagnose and fix during startup.

bin/router/src/supergraph/base.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ pub enum LoadSupergraphError {
1515
InitializationError(String),
1616
#[error("Invalid configuration: {0}")]
1717
InvalidConfiguration(String),
18+
#[error("Supergraph file path is missing. Please provide it via 'SUPERGRAPH_FILE_PATH' environment variable or under 'supergraph.path' in the configuration.")]
19+
MissingSupergraphFilePath,
20+
#[error("Hive CDN endpoint is missing. Please provide it via 'HIVE_CDN_ENDPOINT' environment variable or under 'supergraph.endpoint' in the configuration.")]
21+
MissingHiveCDNEndpoint,
22+
#[error("Hive CDN key is missing. Please provide it via 'HIVE_CDN_KEY' environment variable or under 'supergraph.key' in the configuration.")]
23+
MissingHiveCDNKey,
1824
}
1925

2026
#[derive(Debug)]

bin/router/src/supergraph/mod.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ pub fn resolve_from_config(
2323
SupergraphSource::File {
2424
path,
2525
poll_interval,
26-
} => Ok(SupergraphFileLoader::new(path, *poll_interval)?),
26+
} => {
27+
let path = path
28+
.as_ref()
29+
.ok_or(LoadSupergraphError::MissingSupergraphFilePath)?;
30+
Ok(SupergraphFileLoader::new(path, *poll_interval)?)
31+
}
2732
SupergraphSource::HiveConsole {
2833
endpoint,
2934
key,
@@ -32,14 +37,21 @@ pub fn resolve_from_config(
3237
accept_invalid_certs,
3338
retry_policy,
3439
poll_interval,
35-
} => Ok(SupergraphHiveConsoleLoader::try_new(
36-
endpoint.clone(),
37-
key,
38-
*poll_interval,
39-
*connect_timeout,
40-
*request_timeout,
41-
*accept_invalid_certs,
42-
retry_policy.max_retries,
43-
)?),
40+
} => {
41+
let endpoint = endpoint
42+
.as_ref()
43+
.ok_or(LoadSupergraphError::MissingHiveCDNEndpoint)?;
44+
let key = key.as_ref().ok_or(LoadSupergraphError::MissingHiveCDNKey)?;
45+
46+
Ok(SupergraphHiveConsoleLoader::try_new(
47+
endpoint.clone(),
48+
key,
49+
*poll_interval,
50+
*connect_timeout,
51+
*request_timeout,
52+
*accept_invalid_certs,
53+
retry_policy.max_retries,
54+
)?)
55+
}
4456
}
4557
}

docs/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,7 @@ The path can be either absolute or relative to the router's working directory.
17601760

17611761
|Name|Type|Description|Required|
17621762
|----|----|-----------|--------|
1763-
|**path**|`string`|The path to the supergraph file.<br/><br/>Can also be set using the `SUPERGRAPH_FILE_PATH` environment variable.<br/>Format: `"path"`<br/>|yes|
1763+
|**path**|`string`, `null`|The path to the supergraph file.<br/><br/>Can also be set using the `SUPERGRAPH_FILE_PATH` environment variable.<br/>Format: `"path"`<br/>|no|
17641764
|**poll\_interval**|`string`|Optional interval at which the file should be polled for changes.<br/>If not provided, the file will only be loaded once when the router starts.<br/>|no|
17651765
|**source**|`string`|Constant Value: `"file"`<br/>|yes|
17661766

@@ -1784,8 +1784,8 @@ Loads a supergraph from Hive Console CDN.
17841784
|----|----|-----------|--------|
17851785
|**accept\_invalid\_certs**|`boolean`|Whether to accept invalid TLS certificates when connecting to the Hive Console CDN.<br/>Default: `false`<br/>|no|
17861786
|**connect\_timeout**|`string`|Connect timeout for the Hive Console CDN requests.<br/>Default: `"10s"`<br/>|no|
1787-
|**endpoint**|`string`|The CDN endpoint from Hive Console target.<br/><br/>Can also be set using the `HIVE_CDN_ENDPOINT` environment variable.<br/>|yes|
1788-
|**key**|`string`|The CDN Access Token with from the Hive Console target.<br/><br/>Can also be set using the `HIVE_CDN_KEY` environment variable.<br/>|yes|
1787+
|**endpoint**|`string`, `null`|The CDN endpoint from Hive Console target.<br/><br/>Can also be set using the `HIVE_CDN_ENDPOINT` environment variable.<br/>|no|
1788+
|**key**|`string`, `null`|The CDN Access Token with from the Hive Console target.<br/><br/>Can also be set using the `HIVE_CDN_KEY` environment variable.<br/>|no|
17891789
|**poll\_interval**|`string`|Interval at which the Hive Console should be polled for changes.<br/><br/>Can also be set using the `HIVE_CDN_POLL_INTERVAL` environment variable.<br/>Default: `"10s"`<br/>|no|
17901790
|**request\_timeout**|`string`|Request timeout for the Hive Console CDN requests.<br/>Default: `"1m"`<br/>|no|
17911791
|[**retry\_policy**](#option2retry_policy)|`object`|Interval at which the Hive Console should be polled for changes.<br/>Default: `{"max_retries":10}`<br/>|yes|

lib/router-config/src/supergraph.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub enum SupergraphSource {
1515
/// The path to the supergraph file.
1616
///
1717
/// Can also be set using the `SUPERGRAPH_FILE_PATH` environment variable.
18-
path: FilePath,
18+
path: Option<FilePath>,
1919
/// Optional interval at which the file should be polled for changes.
2020
/// If not provided, the file will only be loaded once when the router starts.
2121
#[serde(
@@ -32,11 +32,11 @@ pub enum SupergraphSource {
3232
/// The CDN endpoint from Hive Console target.
3333
///
3434
/// Can also be set using the `HIVE_CDN_ENDPOINT` environment variable.
35-
endpoint: String,
35+
endpoint: Option<String>,
3636
/// The CDN Access Token with from the Hive Console target.
3737
///
3838
/// Can also be set using the `HIVE_CDN_KEY` environment variable.
39-
key: String,
39+
key: Option<String>,
4040
/// Interval at which the Hive Console should be polled for changes.
4141
///
4242
/// Can also be set using the `HIVE_CDN_POLL_INTERVAL` environment variable.
@@ -110,8 +110,7 @@ fn default_file_poll_interval() -> Option<Duration> {
110110
impl Default for SupergraphSource {
111111
fn default() -> Self {
112112
SupergraphSource::File {
113-
path: FilePath::new_from_relative("supergraph.graphql")
114-
.expect("failed to resolve local path for supergraph file source"),
113+
path: FilePath::new_from_relative("supergraph.graphql").ok(),
115114
poll_interval: default_file_poll_interval(),
116115
}
117116
}

0 commit comments

Comments
 (0)