diff options
| author | Armand Philippot <git@armandphilippot.com> | 2021-12-17 23:29:10 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2021-12-17 23:48:06 +0100 |
| commit | 50d37beeb51c95aaead8b3ef2c946189a066486e (patch) | |
| tree | 61b812d884251767e3ffd4a99d76968d680f9bd0 /src/services | |
| parent | efed6c0a820c5c47e097fa29455157bbd318ffca (diff) | |
chore: create mutation to add a new comment on posts
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/graphql/comments.ts | 68 | ||||
| -rw-r--r-- | src/services/graphql/post.ts | 1 |
2 files changed, 69 insertions, 0 deletions
diff --git a/src/services/graphql/comments.ts b/src/services/graphql/comments.ts new file mode 100644 index 0000000..b7a9ed2 --- /dev/null +++ b/src/services/graphql/comments.ts @@ -0,0 +1,68 @@ +import { + CreatedCommentResponse, + CreatedCommentReturn, +} from '@ts/types/comments'; +import { gql } from 'graphql-request'; +import { getGraphQLClient } from './client'; + +export const createComment: CreatedCommentReturn = async ( + author: string, + authorEmail: string, + authorUrl: string, + content: string, + parent: number, + commentOn: number, + mutationId: string +) => { + const client = getGraphQLClient(); + const mutation = gql` + mutation CreateComment( + $author: String! + $authorEmail: String! + $authorUrl: String! + $content: String! + $parent: ID! + $commentOn: Int! + $mutationId: String! + ) { + createComment( + input: { + author: $author + authorEmail: $authorEmail + authorUrl: $authorUrl + content: $content + parent: $parent + commentOn: $commentOn + clientMutationId: $mutationId + } + ) { + clientMutationId + success + comment { + approved + } + } + } + `; + + const variables = { + author, + authorEmail, + authorUrl, + content, + parent, + commentOn, + mutationId, + }; + + try { + const response: CreatedCommentResponse = await client.request( + mutation, + variables + ); + return response.createComment; + } catch (error) { + console.error(error, undefined, 2); + throw new Error(`An uncaught exception has occurred: ${error}`); + } +}; diff --git a/src/services/graphql/post.ts b/src/services/graphql/post.ts index 245bf7a..08411bf 100644 --- a/src/services/graphql/post.ts +++ b/src/services/graphql/post.ts @@ -65,6 +65,7 @@ const fetchPostBySlug: FetchPostByReturn = async (slug: string) => { afterMore beforeMore } + databaseId date featuredImage { node { |
