Update to use autogenerated Comment Query

- using the custom CommentByPost Query was not returning the author of the Post, therefore the users avatar was not showing up
- there are some weird bugs with the comments, if you dblclick on the button, two comments with the same content are created
- sometimes the comments appear as the bottom of the list, sometimes they appear three comments from the bottom(?)
This commit is contained in:
Matt Rider 2019-05-02 19:55:54 -03:00
parent 154082a251
commit 346158da7f
3 changed files with 38 additions and 39 deletions

View File

@ -3,26 +3,6 @@ import { UserInputError } from 'apollo-server'
const COMMENT_MIN_LENGTH = 1
export default {
Query: {
CommentByPost: async (object, params, context, resolveInfo) => {
const { postId } = params
const session = context.driver.session()
const transactionRes = await session.run(`
MATCH (comment:Comment)-[:COMMENTS]->(post:Post {id: $postId})
RETURN comment {.id, .contentExcerpt, .createdAt} ORDER BY comment.createdAt ASC`, {
postId
})
session.close()
let comments = []
transactionRes.records.map(record => {
comments.push(record.get('comment'))
})
return comments
}
},
Mutation: {
CreateComment: async (object, params, context, resolveInfo) => {
const content = params.content.replace(/<(?:.|\n)*?>/gm, '').trim()
@ -31,29 +11,26 @@ export default {
throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} character long!`)
}
const { postId } = params
delete params.postId
const comment = await neo4jgraphql(object, params, context, resolveInfo, false)
const session = context.driver.session()
await session.run(`
MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId})
MERGE (post)<-[:COMMENTS]-(comment)
RETURN comment {.id, .content}`, {
MATCH (author:User {id: $userId}), (comment:Comment {id: $commentId})
MERGE (comment)<-[:WROTE]-(author)
RETURN author`, {
userId: context.user.id,
commentId: comment.id
}
)
await session.run(`
MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId})
MERGE (post)<-[:COMMENTS]-(comment)
RETURN post`, {
postId,
commentId: comment.id
}
)
await session.run(`
MATCH (comment:Comment {id: $commentId}), (author:User {id: $userId})
MERGE (comment)<-[:WROTE]-(author)
RETURN comment {.id, .content}`, {
commentId: comment.id,
userId: context.user.id
}
)
session.close()
return comment

View File

@ -1,12 +1,34 @@
import gql from 'graphql-tag'
export default app => {
const lang = app.$i18n.locale().toUpperCase()
return gql(`
query CommentByPost($postId: ID!) {
CommentByPost(postId: $postId) {
query Comment($postId: ID) {
Comment(postId: $postId) {
id
contentExcerpt
createdAt
author {
id
slug
name
avatar
disabled
deleted
shoutedCount
contributionsCount
commentsCount
followedByCount
followedByCurrentUser
location {
name: name${lang}
}
badges {
id
key
icon
}
}
}
}
`)

View File

@ -179,7 +179,7 @@ export default {
this.post = post[0] || {}
this.title = this.post.title
},
CommentByPost(comments) {
Comment(comments) {
this.comments = comments || []
}
},
@ -291,11 +291,11 @@ export default {
return this.$store.getters['auth/user'].id === id
},
addComment(comment) {
this.$apollo.queries.CommentByPost.refetch()
this.$apollo.queries.Comment.refetch()
}
},
apollo: {
CommentByPost: {
Comment: {
query() {
return require('~/graphql/CommentQuery.js').default(this)
},