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 creation #221

Merged
merged 5 commits into from
Sep 16, 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
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,
})
}
Loading