Skip to content

Commit

Permalink
feat(client): post creation (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Liendo authored Sep 16, 2024
1 parent 816e38c commit 598fd8a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/client/src/modules/post/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
pub mod post_create;
pub mod posts;

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

use crate::post::post_create::post_create::PostCreateInput;

pub struct PostClient {
client: Client,
domain: Url,
Expand All @@ -17,6 +20,10 @@ impl PostClient {
}
}

pub async fn post_create(&self, input: PostCreateInput) -> Result<post_create::PostCreate> {
post_create::posts(self, input).await
}

pub async fn posts(
&self,
after: Option<Pxid>,
Expand Down
31 changes: 31 additions & 0 deletions crates/client/src/modules/post/post_create/PostCreate.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mutation PostCreate($input: PostCreateInput!) {
postCreate(input: $input) {
post {
id
authorId
parentId
head
title
content
createdAt
updatedAt
author {
id
name
surname
username
email
createdAt
updatedAt
avatar {
id
url
}
}
}
error {
code
message
}
}
}
38 changes: 38 additions & 0 deletions crates/client/src/modules/post/post_create/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use anyhow::{anyhow, Result};
use graphql_client::reqwest::post_graphql;
use graphql_client::GraphQLQuery;
use pxid::Pxid;

use post_create::{
PostCreateInput, PostCreatePostCreateError, PostCreatePostCreatePost, 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/post_create/PostCreate.gql"
)]
pub struct PostCreate {
pub post: Option<PostCreatePostCreatePost>,
pub error: Option<PostCreatePostCreateError>,
}

pub async fn posts(post_client: &PostClient, input: PostCreateInput) -> Result<PostCreate> {
let url = post_client.domain.join(GRAPHQL_PATH)?;
let variables = Variables { input };

let res = post_graphql::<PostCreate, _>(&post_client.client, url, variables)
.await
.map_err(|err| anyhow!("Failed to create post. {err}"))?;
let data = res.data.unwrap().post_create;

Ok(PostCreate {
post: data.post,
error: data.error,
})
}

0 comments on commit 598fd8a

Please sign in to comment.