From 598fd8a28168892ce305e8a1b631e05f882edf84 Mon Sep 17 00:00:00 2001 From: Michael Liendo Date: Sun, 15 Sep 2024 21:51:54 -0400 Subject: [PATCH] feat(client): post creation (#221) --- crates/client/src/modules/post/mod.rs | 7 ++++ .../modules/post/post_create/PostCreate.gql | 31 +++++++++++++++ .../src/modules/post/post_create/mod.rs | 38 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 crates/client/src/modules/post/post_create/PostCreate.gql create mode 100644 crates/client/src/modules/post/post_create/mod.rs diff --git a/crates/client/src/modules/post/mod.rs b/crates/client/src/modules/post/mod.rs index 07d090b..f0f4d92 100644 --- a/crates/client/src/modules/post/mod.rs +++ b/crates/client/src/modules/post/mod.rs @@ -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, @@ -17,6 +20,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/PostCreate.gql b/crates/client/src/modules/post/post_create/PostCreate.gql new file mode 100644 index 0000000..3f1343d --- /dev/null +++ b/crates/client/src/modules/post/post_create/PostCreate.gql @@ -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 + } + } +} 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..a08001b --- /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(post_client: &PostClient, input: PostCreateInput) -> Result { + let url = post_client.domain.join(GRAPHQL_PATH)?; + let variables = Variables { input }; + + let res = post_graphql::(&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, + }) +}