diff --git a/crates/client/src/modules/post/mod.rs b/crates/client/src/modules/post/mod.rs index 07d090b..0f68b93 100644 --- a/crates/client/src/modules/post/mod.rs +++ b/crates/client/src/modules/post/mod.rs @@ -1,6 +1,8 @@ +pub mod post_create; pub mod posts; use anyhow::Result; +use post_create::post_create::PostCreateInput; use pxid::Pxid; use reqwest::{Client, Url}; @@ -17,6 +19,10 @@ impl PostClient { } } + pub async fn post_create(&self, input: PostCreateInput) -> Result { + post_create::posts(self, input).await + } + pub async fn posts( &self, after: Option, diff --git a/crates/client/src/modules/post/post_create/mod.rs b/crates/client/src/modules/post/post_create/mod.rs new file mode 100644 index 0000000..df81d67 --- /dev/null +++ b/crates/client/src/modules/post/post_create/mod.rs @@ -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, + pub error: Option, +} + +pub async fn posts(public_client: &PostClient, input: PostCreateInput) -> Result { + let url = public_client.domain.join(GRAPHQL_PATH)?; + let variables = Variables { input }; + + let res = post_graphql::(&public_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, + }) +}