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(client): post query #220

Merged
merged 3 commits into from
Sep 15, 2024
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
3 changes: 3 additions & 0 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ use anyhow::{anyhow, Result};
use reqwest::Url;

use auth::AuthClient;
use post::PostClient;

pub struct Client {
pub auth: AuthClient,
pub post: PostClient,
}

impl Client {
Expand All @@ -24,6 +26,7 @@ impl Client {

Ok(Self {
auth: AuthClient::new(domain.clone()),
post: PostClient::new(domain.clone()),
})
}
}
1 change: 1 addition & 0 deletions crates/client/src/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod auth;
pub mod post;

pub(crate) type DateTime = chrono::DateTime<chrono::Utc>;
29 changes: 29 additions & 0 deletions crates/client/src/modules/post/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pub mod posts;

use anyhow::Result;
use pxid::Pxid;
use reqwest::{Client, Url};

pub struct PostClient {
client: Client,
domain: Url,
}

impl PostClient {
pub fn new(domain: Url) -> Self {
Self {
domain,
client: Client::new(),
}
}

pub async fn posts(
&self,
after: Option<Pxid>,
before: Option<Pxid>,
first: Option<i64>,
last: Option<i64>,
) -> Result<posts::Posts> {
posts::posts(self, after, before, first, last).await
}
}
34 changes: 34 additions & 0 deletions crates/client/src/modules/post/posts/Posts.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
query Posts($after: Pxid, $before: Pxid, $first: Int, $last: Int) {
posts(after: $after, before: $before, first: $first, last: $last) {
edges {
node {
id
title
content
createdAt
updatedAt
author {
id
name
surname
username
email
createdAt
updatedAt
avatar {
id
url
}
}
}
cursor
}
totalCount
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
}
55 changes: 55 additions & 0 deletions crates/client/src/modules/post/posts/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use anyhow::{anyhow, bail, Result};
use graphql_client::reqwest::post_graphql;
use graphql_client::GraphQLQuery;
use pxid::Pxid;

use posts::{PostsPostsEdges, PostsPostsPageInfo, Variables};

use crate::{DateTime, GRAPHQL_PATH};

use super::PostClient;

#[derive(GraphQLQuery)]
#[graphql(
response_derives = "Clone,Debug,Deserialize",
schema_path = "schema.json",
query_path = "src/modules/post/posts/Posts.gql"
)]
pub struct Posts {
pub edges: Vec<PostsPostsEdges>,
pub total_count: i64,
pub page_info: PostsPostsPageInfo,
}

pub async fn posts(
public_client: &PostClient,
after: Option<Pxid>,
before: Option<Pxid>,
first: Option<i64>,
last: Option<i64>,
) -> Result<Posts> {
let url = public_client.domain.join(GRAPHQL_PATH)?;

let res = post_graphql::<Posts, _>(
&public_client.client,
url,
Variables {
after,
before,
first,
last,
},
)
.await
.map_err(|err| anyhow!("Failed to make request. {err}"))?;

if let Some(ref data) = res.data {
return Ok(Posts {
edges: data.posts.edges.to_owned(),
total_count: data.posts.total_count,
page_info: data.posts.page_info.to_owned(),
});
}

bail!("Failed to get posts. err = {:#?}", res.errors.unwrap())
}
2 changes: 1 addition & 1 deletion crates/web/src/components/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use leptos::{

use townhall_client::Client;

use crate::components::button::{Button, ButtonType, ButtonVariant};
use crate::components::button::{Button, ButtonType};
use crate::components::text_field::{TextField, TextFieldType};

#[component]
Expand Down
2 changes: 1 addition & 1 deletion crates/web/src/components/auth/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use leptos::{
use townhall_client::Client;
use townhall_types::user::Email;

use crate::components::button::{Button, ButtonType, ButtonVariant};
use crate::components::button::{Button, ButtonType};
use crate::components::text_field::{TextField, TextFieldType};

#[component]
Expand Down
2 changes: 1 addition & 1 deletion crates/web/src/components/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn Button(
#[prop(optional, into)] full_width: MaybeProp<bool>,
#[prop(optional, into)] variant: MaybeProp<ButtonVariant>,
#[prop(optional)] children: Option<Children>,
#[prop(optional, into)] on_click: MaybeProp<Rc<dyn Fn(MouseEvent) -> () + 'static>>,
#[prop(optional, into)] on_click: MaybeProp<Rc<dyn Fn(MouseEvent) + 'static>>,
#[prop(optional, into)] r#type: ButtonType,
) -> impl IntoView {
let custom_classes = class.get();
Expand Down
Loading