This repository was archived by the owner on Feb 27, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +80
-1
lines changed Expand file tree Collapse file tree 3 files changed +80
-1
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -10,7 +10,8 @@ const wpDataEndpointBase = '/wp'
10
10
11
11
// Define Frontend WP API data endpoints.
12
12
export const wpDataEndpoints = {
13
- archive : `${ wpDataEndpointBase } /archive`
13
+ archive : `${ wpDataEndpointBase } /archive` ,
14
+ comments : `${ wpDataEndpointBase } /comments`
14
15
}
15
16
16
17
let wpApolloClient
You can’t perform that action at this time.
0 commit comments