Skip to content

Commit

Permalink
registry: Move queries from RegistryClient methods functions in api m…
Browse files Browse the repository at this point in the history
…odule
  • Loading branch information
theduke committed Feb 28, 2023
1 parent d6a94d6 commit 830f1bd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 63 deletions.
54 changes: 54 additions & 0 deletions lib/registry/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use anyhow::Context;

use crate::RegistryClient;

use crate::graphql::mutations;
use crate::types::{PublishDeployAppOutput, PublishDeployAppRawVars};

/// Generate a Deploy token for for the given Deploy app version id.
pub async fn generate_deploy_token(
client: &RegistryClient,
app_version_id: String,
) -> Result<String, anyhow::Error> {
let vars = mutations::generate_deploy_token::Variables { app_version_id };
let res = client
.execute::<mutations::GenerateDeployToken>(vars)
.await?;
let token = res
.generate_deploy_token
.context("Query did not return a token")?
.token;

Ok(token)
}

/// Publish a Deploy app.
///
/// Takes a raw, unvalidated deployment config.
// TODO: Add a variant of this query that takes a typed DeployV1 config.
pub async fn publish_deploy_app_raw(
client: &RegistryClient,
data: PublishDeployAppRawVars,
) -> Result<PublishDeployAppOutput, anyhow::Error> {
let vars2 = mutations::publish_deploy_app::Variables {
name: data.name,
owner: data.namespace,
config: serde_json::to_string(&data.config)?,
};

let version = client
.execute::<mutations::PublishDeployApp>(vars2)
.await?
.publish_deploy_app
.context("Query did not return data")?
.deploy_app_version;
let app = version.app.context("Query did not return expected data")?;

Ok(PublishDeployAppOutput {
app_id: app.id,
app_name: app.name,
version_id: version.id,
version_name: version.version,
owner_name: app.owner.global_name,
})
}
78 changes: 16 additions & 62 deletions lib/registry/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ use anyhow::Context;
use graphql_client::GraphQLQuery;
use url::Url;

use crate::{
graphql,
types::{PublishDeployAppOutput, PublishDeployAppRawVars},
};

/// API client for the Wasmer registry.
#[derive(Clone)]
pub struct RegistryClient {
Expand Down Expand Up @@ -60,7 +55,7 @@ impl RegistryClient {
}

/// Execute a GraphQL query.
async fn execute<Q: GraphQLQuery>(
pub(crate) async fn execute_unchecked<Q: GraphQLQuery>(
&self,
vars: Q::Variables,
) -> Result<graphql_client::Response<Q::ResponseData>, reqwest::Error> {
Expand All @@ -82,65 +77,24 @@ impl RegistryClient {
}

/// Execute a GraphQL query, and convert a response with errors to a Rust error.
async fn execute_checked<Q: GraphQLQuery>(
pub(crate) async fn execute<Q: GraphQLQuery>(
&self,
vars: Q::Variables,
) -> Result<Q::ResponseData, anyhow::Error> {
let res = self.execute::<Q>(vars).await?;

if let Some(data) = res.data {
Ok(data)
} else {
// TODO: Better error forwaring with a custom error type.
anyhow::bail!("GraphQL error: {:?}", res.errors);
let res = self.execute_unchecked::<Q>(vars).await?;

match (res.data, res.errors) {
(_, Some(errors)) => {
// TODO: Better error forwaring with a custom error type.
let errors = errors
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(", ");
anyhow::bail!("GraphQL error: {errors}");
}
(Some(data), None) => Ok(data),
(None, None) => anyhow::bail!("GraphQL response contained no data"),
}
}

/// Generate a Deploy token for for the given Deploy app version id.
pub async fn generate_deploy_token(
&self,
app_version_id: String,
) -> Result<String, anyhow::Error> {
let vars = graphql::mutations::generate_deploy_token::Variables { app_version_id };
let res = self
.execute_checked::<graphql::mutations::GenerateDeployToken>(vars)
.await?;
let token = res
.generate_deploy_token
.context("Query did not return a token")?
.token;

Ok(token)
}

/// Publish a Deploy app.
///
/// Takes a raw, unvalidated deployment config.
// TODO: Add a variant of this query that takes a typed DeployV1 config.
pub async fn publish_deploy_app_raw(
&self,
data: PublishDeployAppRawVars,
) -> Result<PublishDeployAppOutput, anyhow::Error> {
let vars2 = graphql::mutations::publish_deploy_app::Variables {
name: data.name,
owner: data.namespace,
config: serde_json::to_string(&data.config)?,
};

let version = self
.execute_checked::<graphql::mutations::PublishDeployApp>(vars2)
.await?
.publish_deploy_app
.context("Query did not return data")?
.deploy_app_version;
let app = version.app.context("Query did not return expected data")?;

Ok(PublishDeployAppOutput {
app_id: app.id,
app_name: app.name,
version_id: version.id,
version_name: version.version,
owner_name: app.owner.global_name,
})
}
}
3 changes: 2 additions & 1 deletion lib/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! curl -sSfL https://registry.wapm.io/graphql/schema.graphql > lib/registry/graphql/schema.graphql
//! ```
pub mod api;
mod client;
pub mod config;
pub mod graphql;
Expand All @@ -18,7 +19,7 @@ pub mod publish;
pub mod types;
pub mod utils;

pub use crate::client::RegistryClient;
pub use client::RegistryClient;

use anyhow::Context;
use core::ops::Range;
Expand Down

0 comments on commit 830f1bd

Please sign in to comment.