mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Merge pull request #901 from Human-Connection/900-refactor-create-comments-mutation-remove-global-event-listener-update-cache
Refactor CreateComments functionality
This commit is contained in:
commit
ef9b43f1da
@ -38,22 +38,41 @@ export default {
|
|||||||
if (!post) {
|
if (!post) {
|
||||||
throw new UserInputError(NO_POST_ERR_MESSAGE)
|
throw new UserInputError(NO_POST_ERR_MESSAGE)
|
||||||
}
|
}
|
||||||
const comment = await neo4jgraphql(object, params, context, resolveInfo, false)
|
const commentWithoutRelationships = await neo4jgraphql(
|
||||||
|
object,
|
||||||
|
params,
|
||||||
|
context,
|
||||||
|
resolveInfo,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
|
||||||
await session.run(
|
let transactionRes = await session.run(
|
||||||
`
|
`
|
||||||
MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId}), (author:User {id: $userId})
|
MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId}), (author:User {id: $userId})
|
||||||
MERGE (post)<-[:COMMENTS]-(comment)<-[:WROTE]-(author)
|
MERGE (post)<-[:COMMENTS]-(comment)<-[:WROTE]-(author)
|
||||||
RETURN post`,
|
RETURN comment, author`,
|
||||||
{
|
{
|
||||||
userId: context.user.id,
|
userId: context.user.id,
|
||||||
postId,
|
postId,
|
||||||
commentId: comment.id,
|
commentId: commentWithoutRelationships.id,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
session.close()
|
|
||||||
|
|
||||||
return comment
|
const [commentWithAuthor] = transactionRes.records.map(record => {
|
||||||
|
return {
|
||||||
|
comment: record.get('comment'),
|
||||||
|
author: record.get('author'),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const { comment, author } = commentWithAuthor
|
||||||
|
|
||||||
|
const commentReturnedWithAuthor = {
|
||||||
|
...comment.properties,
|
||||||
|
author: author.properties,
|
||||||
|
}
|
||||||
|
session.close()
|
||||||
|
return commentReturnedWithAuthor
|
||||||
},
|
},
|
||||||
DeleteComment: async (object, params, context, resolveInfo) => {
|
DeleteComment: async (object, params, context, resolveInfo) => {
|
||||||
const comment = await neo4jgraphql(object, params, context, resolveInfo, false)
|
const comment = await neo4jgraphql(object, params, context, resolveInfo, false)
|
||||||
|
|||||||
@ -25,6 +25,8 @@
|
|||||||
<script>
|
<script>
|
||||||
import gql from 'graphql-tag'
|
import gql from 'graphql-tag'
|
||||||
import HcEditor from '~/components/Editor'
|
import HcEditor from '~/components/Editor'
|
||||||
|
import PostCommentsQuery from '~/graphql/PostCommentsQuery.js'
|
||||||
|
import CommentMutations from '~/graphql/CommentMutations.js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
@ -62,22 +64,22 @@ export default {
|
|||||||
this.disabled = true
|
this.disabled = true
|
||||||
this.$apollo
|
this.$apollo
|
||||||
.mutate({
|
.mutate({
|
||||||
mutation: gql`
|
mutation: CommentMutations().CreateComment,
|
||||||
mutation($postId: ID, $content: String!) {
|
|
||||||
CreateComment(postId: $postId, content: $content) {
|
|
||||||
id
|
|
||||||
content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`,
|
|
||||||
variables: {
|
variables: {
|
||||||
postId: this.post.id,
|
postId: this.post.id,
|
||||||
content: this.form.content,
|
content: this.form.content,
|
||||||
},
|
},
|
||||||
|
update: (store, { data: { CreateComment } }) => {
|
||||||
|
const data = store.readQuery({
|
||||||
|
query: PostCommentsQuery(this.$i18n),
|
||||||
|
variables: { slug: this.post.slug },
|
||||||
|
})
|
||||||
|
data.Post[0].comments.push(CreateComment)
|
||||||
|
store.writeQuery({ query: PostCommentsQuery(this.$i18n), data })
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
this.loading = false
|
this.loading = false
|
||||||
this.$root.$emit('refetchPostComments')
|
|
||||||
this.clear()
|
this.clear()
|
||||||
this.$toast.success(this.$t('post.comment.submitted'))
|
this.$toast.success(this.$t('post.comment.submitted'))
|
||||||
this.disabled = false
|
this.disabled = false
|
||||||
@ -94,6 +96,8 @@ export default {
|
|||||||
User(orderBy: slug_asc) {
|
User(orderBy: slug_asc) {
|
||||||
id
|
id
|
||||||
slug
|
slug
|
||||||
|
name
|
||||||
|
avatar
|
||||||
}
|
}
|
||||||
}`)
|
}`)
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { mount, createLocalVue, createWrapper } from '@vue/test-utils'
|
import { mount, createLocalVue } from '@vue/test-utils'
|
||||||
import CommentForm from './index.vue'
|
import CommentForm from './index.vue'
|
||||||
import Styleguide from '@human-connection/styleguide'
|
import Styleguide from '@human-connection/styleguide'
|
||||||
import Vuex from 'vuex'
|
import Vuex from 'vuex'
|
||||||
@ -81,11 +81,6 @@ describe('CommentForm.vue', () => {
|
|||||||
expect(cancelMethodSpy).toHaveBeenCalledTimes(1)
|
expect(cancelMethodSpy).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('emits a method call with the returned comment', () => {
|
|
||||||
const rootWrapper = createWrapper(wrapper.vm.$root)
|
|
||||||
expect(rootWrapper.emitted().refetchPostComments.length).toEqual(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('mutation fails', () => {
|
describe('mutation fails', () => {
|
||||||
it('shows the error toaster', async () => {
|
it('shows the error toaster', async () => {
|
||||||
await wrapper.find('form').trigger('submit')
|
await wrapper.find('form').trigger('submit')
|
||||||
|
|||||||
@ -88,10 +88,5 @@ describe('CommentList.vue', () => {
|
|||||||
it('displays comments when there are comments to display', () => {
|
it('displays comments when there are comments to display', () => {
|
||||||
expect(wrapper.find('div.comments').text()).toEqual('this is a comment')
|
expect(wrapper.find('div.comments').text()).toEqual('this is a comment')
|
||||||
})
|
})
|
||||||
|
|
||||||
it("refetches a post's comments from the backend", () => {
|
|
||||||
wrapper.vm.refetchPostComments()
|
|
||||||
expect(mocks.$apollo.queries.Post.refetch).toHaveBeenCalledTimes(1)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -30,6 +30,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import Comment from '~/components/Comment.vue'
|
import Comment from '~/components/Comment.vue'
|
||||||
import HcEmpty from '~/components/Empty.vue'
|
import HcEmpty from '~/components/Empty.vue'
|
||||||
|
import PostCommentsQuery from '~/graphql/PostCommentsQuery.js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
@ -49,25 +50,10 @@ export default {
|
|||||||
this.comments = post[0].comments || []
|
this.comments = post[0].comments || []
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mounted() {
|
|
||||||
this.$root.$on('refetchPostComments', () => {
|
|
||||||
this.refetchPostComments()
|
|
||||||
})
|
|
||||||
},
|
|
||||||
beforeDestroy() {
|
|
||||||
this.$root.$off('refetchPostComments')
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
refetchPostComments() {
|
|
||||||
if (this.$apollo.queries.Post) {
|
|
||||||
this.$apollo.queries.Post.refetch()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
apollo: {
|
apollo: {
|
||||||
Post: {
|
Post: {
|
||||||
query() {
|
query() {
|
||||||
return require('~/graphql/PostCommentsQuery.js').default(this)
|
return PostCommentsQuery(this.$i18n)
|
||||||
},
|
},
|
||||||
variables() {
|
variables() {
|
||||||
return {
|
return {
|
||||||
|
|||||||
23
webapp/graphql/CommentMutations.js
Normal file
23
webapp/graphql/CommentMutations.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
return {
|
||||||
|
CreateComment: gql`
|
||||||
|
mutation($postId: ID, $content: String!) {
|
||||||
|
CreateComment(postId: $postId, content: $content) {
|
||||||
|
id
|
||||||
|
contentExcerpt
|
||||||
|
author {
|
||||||
|
id
|
||||||
|
slug
|
||||||
|
name
|
||||||
|
avatar
|
||||||
|
}
|
||||||
|
createdAt
|
||||||
|
deleted
|
||||||
|
disabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import gql from 'graphql-tag'
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
export default app => {
|
export default i18n => {
|
||||||
const lang = app.$i18n.locale().toUpperCase()
|
const lang = i18n.locale().toUpperCase()
|
||||||
return gql(`
|
return gql(`
|
||||||
query Post($slug: String!) {
|
query Post($slug: String!) {
|
||||||
Post(slug: $slug) {
|
Post(slug: $slug) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user