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

Commit 0e3ae89

Browse files
committed
Set up bridge to backend
1 parent a2cc907 commit 0e3ae89

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {initializeWpApollo} from '../connector'
2+
import mutationCommentToPost from '../comments/mutationCommentToPost'
3+
4+
/**
5+
* Post a comment to the given post. Follows established WordPress
6+
* behavior for posting comments:
7+
*
8+
* If this is an authenticated request (i.e. "logged in"), the `author`
9+
* fields will be ignored in favor of the logged-in user's information.
10+
*
11+
* If comment moderation is turned on, the `data.createComment.comment`
12+
* object may be `null`. This does not necessarily indicate an error;
13+
* the comment may be held for moderation.
14+
*
15+
* If the comment does not need manual approval, it will be returned
16+
* with this query.
17+
*
18+
* @author WebDevStudios
19+
* @param {string} author Name of the comment author.
20+
* @param {string} authorEmail Email for the comment author.
21+
* @param {string} authorUrl URL/website for the comment author.
22+
* @param {number} postId Database ID for the post being commented on.
23+
* @param {string} content Content of the comment.
24+
* @return {object} Comment data or error object.
25+
*/
26+
export default async function postCommentToPost(
27+
author,
28+
authorEmail,
29+
authorUrl,
30+
postId,
31+
content
32+
) {
33+
// Get/create Apollo instance.
34+
const apolloClient = initializeWpApollo()
35+
36+
// Set up return object.
37+
const response = {
38+
apolloClient,
39+
comment: null
40+
}
41+
42+
// Determine query variables.
43+
const variables = {
44+
author,
45+
authorEmail,
46+
authorUrl,
47+
postId: parseInt(postId),
48+
content
49+
}
50+
51+
// Execute query.
52+
await apolloClient
53+
.mutate({mutation: mutationCommentToPost, variables})
54+
.then((comment) => {
55+
const {data} = comment
56+
// Set error props if data not found.
57+
if (!data?.createComment) {
58+
response.error = true
59+
response.errorMessage = `An error occurred while trying to post the comment.`
60+
61+
return null
62+
}
63+
64+
response.comment = data.createComment.comment
65+
})
66+
.catch((error) => {
67+
response.error = true
68+
response.errorMessage = error.message
69+
})
70+
71+
return response
72+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {gql} from '@apollo/client'
2+
import commentsFields from '../_partials/commentsFields'
3+
4+
// Mutation: Add a comment to the given post
5+
const mutationCommentToPost = gql`
6+
mutation ADD_COMMENT(
7+
$author: String
8+
$authorEmail: String
9+
$authorUrl: String
10+
$postId: Int!
11+
$content: String!
12+
) {
13+
createComment (
14+
input: {
15+
author: $author,
16+
authorEmail: $authorEmail,
17+
authorUrl: $authorUrl,
18+
commentOn: $postId,
19+
content: $content
20+
}
21+
) {
22+
comment {
23+
${commentsFields}
24+
}
25+
}
26+
}
27+
`
28+
29+
export default mutationCommentToPost

api/wordpress/connector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const wpDataEndpointBase = '/wp'
1919
// Define Frontend WP API data endpoints.
2020
export const wpDataEndpoints = {
2121
archive: `${wpDataEndpointBase}/archive`,
22-
comments: `${wpDataEndpointBase}/comments`
22+
postComment: `${wpDataEndpointBase}/postComment`
2323
}
2424

2525
let wpApolloClient

pages/api/wp/postComment.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import postCommentToPost from '@/api/wordpress/_global/postCommentToPost'
2+
3+
/**
4+
* Load more posts for an archive.
5+
*
6+
* @author WebDevStudios
7+
* @param {object} req Instance of http.IncomingMessage.
8+
* @param {object} res Instance of http.ServerResponse.
9+
*/
10+
export default async function postComment(req, res) {
11+
try {
12+
// Retrieve props from request query params.
13+
const {author, authorEmail, authorUrl, postId, content} = req.query
14+
15+
const commentResponse = await postCommentToPost(
16+
author,
17+
authorEmail,
18+
authorUrl,
19+
postId,
20+
content
21+
)
22+
23+
// Check for errors.
24+
if (commentResponse.error) {
25+
throw new Error(commentResponse.errorMessage)
26+
}
27+
28+
// Remove Apollo client from return.
29+
delete commentResponse?.apolloClient
30+
31+
res.status(200).send(commentResponse)
32+
} catch (error) {
33+
res
34+
.status(error?.status || 500)
35+
.end(
36+
error?.message || 'An error occurred while attempted to load more posts'
37+
)
38+
}
39+
}

0 commit comments

Comments
 (0)