Skip to content

Commit

Permalink
feat(backend-api): App template improvements
Browse files Browse the repository at this point in the history
* Add fetch_all_app_templates() helper
* Allow sorting of templates
* Make AppTemplate serializable

NOTE: requires a version bump due to a breaking api change
  • Loading branch information
theduke committed Jun 4, 2024
1 parent 7d05fc5 commit 4a0db61
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/backend-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmer-api"
version = "0.0.28"
version = "0.0.29"
description = "Client library for the Wasmer GraphQL API"
readme = "README.md"
documentation = "https://docs.rs/wasmer-api"
Expand Down
52 changes: 52 additions & 0 deletions lib/backend-api/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,70 @@ pub async fn fetch_app_templates(
client: &WasmerClient,
category_slug: String,
first: i32,
after: Option<String>,
sort_by: Option<types::AppTemplatesSortBy>,
) -> Result<Option<types::AppTemplateConnection>, anyhow::Error> {
client
.run_graphql_strict(types::GetAppTemplatesQuery::build(
GetAppTemplatesQueryVariables {
category_slug,
first,
after,
sort_by,
},
))
.await
.map(|r| r.get_app_templates)
}

/// Fetch all app templates by paginating through the responses.
///
/// Will fetch at most `max` templates.
pub async fn fetch_all_app_templates(
client: &WasmerClient,
page_size: i32,
max: usize,
sort_by: Option<types::AppTemplatesSortBy>,
) -> Result<Vec<types::AppTemplate>, anyhow::Error> {
let mut all = Vec::new();
let mut cursor = None;

loop {
let con = client
.run_graphql_strict(types::GetAppTemplatesQuery::build(
GetAppTemplatesQueryVariables {
category_slug: String::new(),
first: page_size,
sort_by,
after: cursor.take(),
},
))
.await?
.get_app_templates
.context("backend did not return any data")?;

if con.edges.is_empty() {
break;
}

let new = con.edges.into_iter().flatten().filter_map(|edge| edge.node);
all.extend(new);

let next_cursor = con
.page_info
.end_cursor
.filter(|_| con.page_info.has_next_page && all.len() < max);

if let Some(next) = next_cursor {
cursor = Some(next);
} else {
break;
}
}

Ok(all)
}

/// Get a signed URL to upload packages.
pub async fn get_signed_url_for_package_upload(
client: &WasmerClient,
Expand Down
26 changes: 24 additions & 2 deletions lib/backend-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,31 @@ mod queries {
#[arguments(slug: $slug)]
pub get_app_template: Option<AppTemplate>,
}

#[derive(cynic::Enum, Clone, Copy, Debug)]
pub enum AppTemplatesSortBy {
Newest,
Oldest,
Popular,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct GetAppTemplatesQueryVariables {
pub category_slug: String,
pub first: i32,
pub after: Option<String>,
pub sort_by: Option<AppTemplatesSortBy>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "GetAppTemplatesQueryVariables")]
pub struct GetAppTemplatesQuery {
#[arguments(categorySlug: $category_slug, first: $first)]
#[arguments(
categorySlug: $category_slug,
first: $first,
after: $after,
sortBy: $sort_by
)]
pub get_app_templates: Option<AppTemplateConnection>,
}

Expand All @@ -168,21 +183,28 @@ mod queries {
pub cursor: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[derive(serde::Serialize, cynic::QueryFragment, Debug)]
pub struct AppTemplate {
#[serde(rename = "demoUrl")]
pub demo_url: String,
pub language: String,
pub name: String,
pub framework: String,
#[serde(rename = "createdAt")]
pub created_at: DateTime,
pub description: String,
pub id: cynic::Id,
#[serde(rename = "isPublic")]
pub is_public: bool,
#[serde(rename = "repoLicense")]
pub repo_license: String,
pub readme: String,
#[serde(rename = "repoUrl")]
pub repo_url: String,
pub slug: String,
#[serde(rename = "updatedAt")]
pub updated_at: DateTime,
#[serde(rename = "useCases")]
pub use_cases: Jsonstring,
}

Expand Down
2 changes: 1 addition & 1 deletion lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ virtual-mio = { version = "0.3.1", path = "../virtual-io" }
# Wasmer-owned dependencies.

webc = { workspace = true }
wasmer-api = { version = "=0.0.28", path = "../backend-api" }
wasmer-api = { version = "=0.0.29", path = "../backend-api" }
edge-schema.workspace = true
edge-util = { version = "=0.1.0" }
lazy_static = "1.4.0"
Expand Down

0 comments on commit 4a0db61

Please sign in to comment.