Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 33a150c

Browse files
committed
Add post comment mutation
1 parent de4c437 commit 33a150c

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {gql} from '@apollo/client'
2+
import {wpDataEndpoints} from '@/api/wordpress/connector'
3+
import commentsFields from '@/api/wordpress/_partials/commentsFields'
4+
5+
// Mutation: Add a comment to the given post
6+
const mutationAddComment = gql`
7+
mutation ADD_COMMENT(
8+
$author: String
9+
$authorEmail: String
10+
$authorUrl: String
11+
$postId: Int!
12+
$content: String!
13+
) {
14+
createComment (
15+
input: {
16+
author: $author,
17+
authorEmail: $authorEmail,
18+
authorUrl: $authorUrl,
19+
commentOn: $postId,
20+
content: $content
21+
}
22+
) @rest(type: "Comments", path: "${wpDataEndpoints.comments}?{args}") {
23+
comment {
24+
${commentsFields}
25+
}
26+
}
27+
}
28+
`
29+
30+
export default mutationAddComment
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import mutationAddComent from './mutationAddComment'
2+
import {initializeFeApollo} from '../../connector'
3+
4+
/**
5+
* Retrieve next page of posts for post type archive.
6+
*
7+
* @author WebDevStudios
8+
* @param {string} author Name of the comment author.
9+
* @param {string} authorEmail Email for the comment author.
10+
* @param {string} authorUrl URL/website for the comment author.
11+
* @param {number} postId Database ID for the post being commented on.
12+
* @param {string} content Content of the comment.
13+
* @return {object} Comment data or error object.
14+
*/
15+
export default async function postComment(
16+
author,
17+
authorEmail,
18+
authorUrl,
19+
postId,
20+
content
21+
) {
22+
const apolloClient = initializeFeApollo()
23+
24+
return apolloClient
25+
.query({
26+
query: mutationAddComent,
27+
variables: {
28+
author,
29+
authorEmail,
30+
authorUrl,
31+
postId,
32+
content
33+
}
34+
})
35+
.then(
36+
(response) =>
37+
response?.data?.createComment ?? {
38+
error: true,
39+
errorMessage: `An error occurred while trying to post the comment.`
40+
}
41+
)
42+
.catch((error) => {
43+
return {
44+
error: true,
45+
errorMessage: error.message
46+
}
47+
})
48+
}

api/wordpress/connector.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const wpDataEndpointBase = '/wp'
1010

1111
// Define Frontend WP API data endpoints.
1212
export const wpDataEndpoints = {
13-
archive: `${wpDataEndpointBase}/archive`
13+
archive: `${wpDataEndpointBase}/archive`,
14+
comments: `${wpDataEndpointBase}/comments`
1415
}
1516

1617
let wpApolloClient

0 commit comments

Comments
 (0)