Skip to content

Commit

Permalink
fix(cli/app-create): Ask for language instead of framework
Browse files Browse the repository at this point in the history
  • Loading branch information
xdoardo committed Jun 26, 2024
1 parent 5ff0ac6 commit 5750fda
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 26 deletions.
35 changes: 33 additions & 2 deletions lib/backend-api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1248,12 +1248,13 @@ type AppTemplate implements Node {
updatedAt: DateTime!
readme: String!
useCases: JSONString!
language: String!
repoLicense: String!
usingPackage: Package
defaultImage: String
framework: String!
templateFramework: TemplateFramework
language: String!
templateLanguage: TemplateLanguage
}

type AppTemplateCategory implements Node {
Expand All @@ -1276,6 +1277,15 @@ type TemplateFramework implements Node {
slug: String!
}

type TemplateLanguage implements Node {
"""The ID of the object"""
id: ID!
createdAt: DateTime!
updatedAt: DateTime!
name: String!
slug: String!
}

type Collection {
slug: String!
displayName: String!
Expand Down Expand Up @@ -2291,7 +2301,8 @@ type Query {
getDeployApps(sortBy: DeployAppsSortBy, updatedAfter: DateTime, offset: Int, before: String, after: String, first: Int, last: Int): DeployAppConnection!
getAppVersions(sortBy: DeployAppVersionsSortBy, updatedAfter: DateTime, offset: Int, before: String, after: String, first: Int, last: Int): DeployAppVersionConnection!
getTemplateFrameworks(offset: Int, before: String, after: String, first: Int, last: Int): TemplateFrameworkConnection
getAppTemplates(categorySlug: String, frameworkSlug: String, sortBy: AppTemplatesSortBy, offset: Int, before: String, after: String, first: Int, last: Int): AppTemplateConnection
getTemplateLanguages(offset: Int, before: String, after: String, first: Int, last: Int): TemplateLanguageConnection
getAppTemplates(categorySlug: String, frameworkSlug: String, languageSlug: String, sortBy: AppTemplatesSortBy, offset: Int, before: String, after: String, first: Int, last: Int): AppTemplateConnection
getAppTemplate(slug: String!): AppTemplate
getAppTemplateCategories(offset: Int, before: String, after: String, first: Int, last: Int): AppTemplateCategoryConnection
viewer: User
Expand Down Expand Up @@ -2410,6 +2421,26 @@ type TemplateFrameworkEdge {
cursor: String!
}

type TemplateLanguageConnection {
"""Pagination data for this connection."""
pageInfo: PageInfo!

"""Contains the nodes in this connection."""
edges: [TemplateLanguageEdge]!

"""Total number of items in the connection."""
totalCount: Int
}

"""A Relay edge containing a `TemplateLanguage` and its cursor."""
type TemplateLanguageEdge {
"""The item at the end of the edge"""
node: TemplateLanguage

"""A cursor for use in pagination"""
cursor: String!
}

enum AppTemplatesSortBy {
NEWEST
OLDEST
Expand Down
143 changes: 138 additions & 5 deletions lib/backend-api/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use crate::{
types::{
self, CreateNamespaceVars, DeployApp, DeployAppConnection, DeployAppVersion,
DeployAppVersionConnection, DnsDomain, GetAppTemplateFromSlugVariables,
GetAppTemplatesFromFrameworkVars, GetAppTemplatesVars, GetCurrentUserWithAppsVars,
GetDeployAppAndVersion, GetDeployAppVersionsVars, GetNamespaceAppsVars,
GetSignedUrlForPackageUploadVariables, GetTemplateFrameworksVars, Log, LogStream,
PackageVersionConnection, PublishDeployAppVars, PushPackageReleasePayload, SignedUrl,
TagPackageReleasePayload, UpsertDomainFromZoneFileVars,
GetAppTemplatesFromFrameworkVars, GetAppTemplatesFromLanguageVars, GetAppTemplatesVars,
GetCurrentUserWithAppsVars, GetDeployAppAndVersion, GetDeployAppVersionsVars,
GetNamespaceAppsVars, GetSignedUrlForPackageUploadVariables, GetTemplateFrameworksVars,
GetTemplateLanguagesVars, Log, LogStream, PackageVersionConnection, PublishDeployAppVars,
PushPackageReleasePayload, SignedUrl, TagPackageReleasePayload,
UpsertDomainFromZoneFileVars,
},
GraphQLApiFailure, WasmerClient,
};
Expand Down Expand Up @@ -167,6 +168,138 @@ pub fn fetch_all_app_templates(
)
}

/// Fetch all app templates by paginating through the responses.
///
/// Will fetch at most `max` templates.
pub fn fetch_all_app_templates_from_language(
client: &WasmerClient,
page_size: i32,
sort_by: Option<types::AppTemplatesSortBy>,
language: String,
) -> impl futures::Stream<Item = Result<Vec<types::AppTemplate>, anyhow::Error>> + '_ {
let vars = GetAppTemplatesFromLanguageVars {
language_slug: language.clone().to_string(),
first: page_size,
sort_by,
after: None,
};

futures::stream::try_unfold(
Some(vars),
move |vars: Option<types::GetAppTemplatesFromLanguageVars>| async move {
let vars = match vars {
Some(vars) => vars,
None => return Ok(None),
};

let con = client
.run_graphql_strict(types::GetAppTemplatesFromLanguage::build(vars.clone()))
.await?
.get_app_templates
.context("backend did not return any data")?;

let items = con
.edges
.into_iter()
.flatten()
.filter_map(|edge| edge.node)
.collect::<Vec<_>>();

let next_cursor = con
.page_info
.end_cursor
.filter(|_| con.page_info.has_next_page);

let next_vars = next_cursor.map(|after| types::GetAppTemplatesFromLanguageVars {
after: Some(after),
..vars
});

#[allow(clippy::type_complexity)]
let res: Result<
Option<(
Vec<types::AppTemplate>,
Option<types::GetAppTemplatesFromLanguageVars>,
)>,
anyhow::Error,
> = Ok(Some((items, next_vars)));

res
},
)
}

/// Fetch languages from available app templates.
pub async fn fetch_app_template_languages(
client: &WasmerClient,
after: Option<String>,
first: Option<i32>,
) -> Result<Option<types::TemplateLanguageConnection>, anyhow::Error> {
client
.run_graphql_strict(types::GetTemplateLanguages::build(
GetTemplateLanguagesVars { after, first },
))
.await
.map(|r| r.get_template_languages)
}

/// Fetch all languages from available app templates by paginating through the responses.
///
/// Will fetch at most `max` templates.
pub fn fetch_all_app_template_languages(
client: &WasmerClient,
page_size: Option<i32>,
) -> impl futures::Stream<Item = Result<Vec<types::TemplateLanguage>, anyhow::Error>> + '_ {
let vars = GetTemplateLanguagesVars {
after: None,
first: page_size,
};

futures::stream::try_unfold(
Some(vars),
move |vars: Option<types::GetTemplateLanguagesVars>| async move {
let vars = match vars {
Some(vars) => vars,
None => return Ok(None),
};

let con = client
.run_graphql_strict(types::GetTemplateLanguages::build(vars.clone()))
.await?
.get_template_languages
.context("backend did not return any data")?;

let items = con
.edges
.into_iter()
.flatten()
.filter_map(|edge| edge.node)
.collect::<Vec<_>>();

let next_cursor = con
.page_info
.end_cursor
.filter(|_| con.page_info.has_next_page);

let next_vars = next_cursor.map(|after| types::GetTemplateLanguagesVars {
after: Some(after),
..vars
});

#[allow(clippy::type_complexity)]
let res: Result<
Option<(
Vec<types::TemplateLanguage>,
Option<types::GetTemplateLanguagesVars>,
)>,
anyhow::Error,
> = Ok(Some((items, next_vars)));

res
},
)
}

/// Fetch all app templates by paginating through the responses.
///
/// Will fetch at most `max` templates.
Expand Down
57 changes: 57 additions & 0 deletions lib/backend-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ mod queries {
pub get_app_templates: Option<AppTemplateConnection>,
}

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

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "GetAppTemplatesFromLanguageVars")]
pub struct GetAppTemplatesFromLanguage {
#[arguments(
languageSlug: $language_slug,
first: $first,
after: $after,
sortBy: $sort_by
)]
pub get_app_templates: Option<AppTemplateConnection>,
}

#[derive(cynic::QueryVariables, Debug, Clone)]
pub struct GetAppTemplatesVars {
pub category_slug: String,
Expand Down Expand Up @@ -272,6 +292,43 @@ mod queries {
pub updated_at: DateTime,
}

#[derive(cynic::QueryVariables, Debug, Clone)]
pub struct GetTemplateLanguagesVars {
pub after: Option<String>,
pub first: Option<i32>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "GetTemplateLanguagesVars")]
pub struct GetTemplateLanguages {
#[arguments(after: $after, first: $first)]
pub get_template_languages: Option<TemplateLanguageConnection>,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct TemplateLanguageConnection {
pub edges: Vec<Option<TemplateLanguageEdge>>,
pub page_info: PageInfo,
pub total_count: Option<i32>,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct TemplateLanguageEdge {
pub cursor: String,
pub node: Option<TemplateLanguage>,
}

#[derive(serde::Serialize, cynic::QueryFragment, PartialEq, Eq, Debug)]
pub struct TemplateLanguage {
#[serde(rename = "createdAt")]
pub created_at: DateTime,
pub id: cynic::Id,
pub name: String,
pub slug: String,
#[serde(rename = "updatedAt")]
pub updated_at: DateTime,
}

#[derive(cynic::Scalar, Debug, Clone, PartialEq, Eq)]
#[cynic(graphql_type = "JSONString")]
pub struct Jsonstring(pub String);
Expand Down
Loading

0 comments on commit 5750fda

Please sign in to comment.