Skip to content

Commit c5d7822

Browse files
dotansimhan1ru4l
andauthored
feat(hive-apollo-router-plugin): support target id and org access token (#6577)
Co-authored-by: Laurin Quast <[email protected]>
1 parent 4f307b9 commit c5d7822

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

.changeset/curvy-mice-joke.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'hive-apollo-router-plugin': minor
3+
---
4+
5+
Add support for providing a target for usage reporting with organization access tokens.
6+
7+
This can either be a slug following the format `$organizationSlug/$projectSlug/$targetSlug` (e.g `the-guild/graphql-hive/staging`)
8+
or an UUID (e.g. `a0f4c605-6541-4350-8cfe-b31f21a4bf80`).
9+
10+
```yaml
11+
# ... other apollo-router configuration
12+
plugins:
13+
hive.usage:
14+
enabled: true
15+
registry_token: "ORGANIZATION_ACCESS_TOKEN"
16+
target: "my-org/my-project/my-target"
17+
```

configs/cargo/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/libraries/router/src/usage.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ pub struct UsagePlugin {
5050
pub struct Config {
5151
/// Default: true
5252
enabled: Option<bool>,
53-
/// Hive token, can also be set using the HIVE_TOKEN environment variable
53+
/// Hive token, can also be set using the HIVE_TOKEN environment variable.
54+
/// The token can be a registry access token, or a organization access token.
5455
registry_token: Option<String>,
5556
/// Hive registry token. Set to your `/usage` endpoint if you are self-hosting.
5657
/// Default: https://app.graphql-hive.com/usage
58+
/// When `target` is set and organization access token is in use, the target ID is appended to the endpoint,
59+
/// so usage endpoint becomes `https://app.graphql-hive.com/usage/<target_id>`
5760
registry_usage_endpoint: Option<String>,
61+
/// The target to which the usage data should be reported to.
62+
/// This can either be a slug following the format "$organizationSlug/$projectSlug/$targetSlug" (e.g "the-guild/graphql-hive/staging")
63+
/// or an UUID (e.g. "a0f4c605-6541-4350-8cfe-b31f21a4bf80").
64+
target: Option<String>,
5865
/// Sample rate to determine sampling.
5966
/// 0.0 = 0% chance of being sent
6067
/// 1.0 = 100% chance of being sent.
@@ -98,6 +105,7 @@ impl Default for Config {
98105
connect_timeout: Some(5),
99106
request_timeout: Some(15),
100107
flush_interval: Some(5),
108+
target: None,
101109
}
102110
}
103111
}
@@ -186,14 +194,25 @@ impl Plugin for UsagePlugin {
186194
return Err("Hive token is required".into());
187195
}
188196

189-
let endpoint = init
197+
let mut endpoint = init
190198
.config
191199
.registry_usage_endpoint
192200
.clone()
193201
.unwrap_or_else(|| {
194202
env::var("HIVE_ENDPOINT").unwrap_or(DEFAULT_HIVE_USAGE_ENDPOINT.to_string())
195203
});
196204

205+
let target_id = init
206+
.config
207+
.target
208+
.clone()
209+
.or_else(|| env::var("HIVE_TARGET_ID").ok());
210+
211+
// In case target ID is specified in configuration, append it to the endpoint
212+
if let Some(target_id) = &target_id {
213+
endpoint.push_str(&format!("/{}", target_id));
214+
}
215+
197216
let default_config = Config::default();
198217
let user_config = init.config;
199218
let enabled = user_config

packages/web/docs/src/content/other-integrations/apollo-router.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ You can send usage reporting to Hive registry by enabling `hive.usage` plugin in
182182
[Registry Access Token](/docs/management/targets#registry-access-tokens) with write permission.
183183
- `HIVE_ENDPOINT` (**optional**) - For self-hosting, you can override `/usage` endpoint (defaults to
184184
`https://app.graphql-hive.com/usage`)
185+
- `HIVE_TARGET_ID` - A target ID, this can either be a slug following the format
186+
"$organizationSlug/$projectSlug/$targetSlug" (e.g "the-guild/graphql-hive/staging") or an UUID
187+
(e.g. "a0f4c605-6541-4350-8cfe-b31f21a4bf80"). To be used when `HIVE_TOKEN` is configured with an
188+
organization access token.
185189

186190
<Tabs items={['Binary', 'Docker', 'Custom']}>
187191
<Tabs.Tab>

0 commit comments

Comments
 (0)