Skip to main content

typed_query/
typed_query.rs

1use infrahub::{Client, ClientConfig};
2use serde::Deserialize;
3use std::env;
4
5#[derive(Debug, Deserialize)]
6#[allow(dead_code)]
7struct Data {
8    #[serde(rename = "InfrahubInfo")]
9    info: InfrahubInfo,
10}
11
12#[derive(Debug, Deserialize)]
13#[allow(dead_code)]
14struct InfrahubInfo {
15    deployment_id: String,
16    version: String,
17}
18
19#[tokio::main]
20async fn main() -> Result<(), Box<dyn std::error::Error>> {
21    let base_url = env::var("INFRAHUB_URL").unwrap_or_else(|_| "http://localhost:8000".to_string());
22    let token = env::var("INFRAHUB_TOKEN").expect("INFRAHUB_TOKEN is required");
23    let branch = env::var("INFRAHUB_BRANCH").ok();
24
25    let config = ClientConfig::new(base_url, token);
26    let client = Client::new(config)?;
27
28    let response = client
29        .execute::<Data>(
30            "query { InfrahubInfo { deployment_id version } }",
31            None,
32            branch.as_deref(),
33        )
34        .await?;
35
36    println!("response: {response:?}");
37    Ok(())
38}