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

feat(backend-api): Add query for retrieving app versions by id #4906

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions lib/backend-api/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,71 @@ pub async fn all_app_versions(
Ok(all_versions)
}

/// Retrieve versions for an app.
pub async fn get_deploy_app_versions_by_id(
client: &WasmerClient,
vars: types::GetDeployAppVersionsByIdVars,
) -> Result<DeployAppVersionConnection, anyhow::Error> {
let res = client
.run_graphql_strict(types::GetDeployAppVersionsById::build(vars))
.await?;
let versions = res
.node
.context("app not found")?
.into_app()
.context("invalid node type returned")?
.versions;
Ok(versions)
}

/// Load all versions of an app id.
///
/// Will paginate through all versions and return them in a single list.
pub async fn all_app_versions_by_id(
client: &WasmerClient,
app_id: impl Into<String>,
) -> Result<Vec<DeployAppVersion>, anyhow::Error> {
let mut vars = types::GetDeployAppVersionsByIdVars {
id: cynic::Id::new(app_id),
offset: None,
before: None,
after: None,
first: Some(10),
last: None,
sort_by: None,
};

let mut all_versions = Vec::<DeployAppVersion>::new();

loop {
let page = get_deploy_app_versions_by_id(client, vars.clone()).await?;
if page.edges.is_empty() {
break;
}

for edge in page.edges {
let edge = match edge {
Some(edge) => edge,
None => continue,
};
let version = match edge.node {
Some(item) => item,
None => continue,
};

// Sanity check to avoid duplication.
if all_versions.iter().any(|v| v.id == version.id) == false {
all_versions.push(version);
}

// Update pagination.
vars.after = Some(edge.cursor);
}
}

Ok(all_versions)
}

/// Activate a particular version of an app.
pub async fn app_version_activate(
client: &WasmerClient,
Expand Down
50 changes: 50 additions & 0 deletions lib/backend-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,39 @@ mod queries {
pub versions: DeployAppVersionConnection,
}

#[derive(cynic::QueryVariables, Debug, Clone)]
pub struct GetDeployAppVersionsByIdVars {
pub id: cynic::Id,

pub offset: Option<i32>,
pub before: Option<String>,
pub after: Option<String>,
pub first: Option<i32>,
pub last: Option<i32>,
pub sort_by: Option<DeployAppVersionsSortBy>,
}

#[derive(cynic::QueryFragment, Debug, Clone, Serialize)]
#[cynic(graphql_type = "DeployApp", variables = "GetDeployAppVersionsByIdVars")]
pub struct DeployAppVersionsById {
#[arguments(
first: $first,
last: $last,
before: $before,
after: $after,
offset: $offset,
sortBy: $sort_by
)]
pub versions: DeployAppVersionConnection,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[cynic(graphql_type = "Query", variables = "GetDeployAppVersionsByIdVars")]
pub struct GetDeployAppVersionsById {
#[arguments(id: $id)]
pub node: Option<NodeDeployAppVersions>,
}

#[derive(cynic::QueryFragment, Serialize, Debug, Clone)]
#[cynic(graphql_type = "DeployApp")]
pub struct SparseDeployApp {
Expand Down Expand Up @@ -1596,6 +1629,23 @@ mod queries {
#[derive(cynic::Scalar, Debug, Clone)]
pub struct BigInt(pub i64);

#[derive(cynic::InlineFragments, Debug, Clone)]
#[cynic(graphql_type = "Node", variables = "GetDeployAppVersionsByIdVars")]
pub enum NodeDeployAppVersions {
DeployApp(Box<DeployAppVersionsById>),
#[cynic(fallback)]
Unknown,
}

impl NodeDeployAppVersions {
pub fn into_app(self) -> Option<DeployAppVersionsById> {
match self {
Self::DeployApp(v) => Some(*v),
_ => None,
}
}
}

#[derive(cynic::InlineFragments, Debug)]
pub enum Node {
DeployApp(Box<DeployApp>),
Expand Down
Loading