mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-12 23:35:58 +00:00
Refactor resolver, rename params
- Use one cypher statement Co-authored-by: Tirokk <wolle.huss@pjannto.com>
This commit is contained in:
parent
351f258fab
commit
865e2c0ec3
@ -1,4 +1,5 @@
|
|||||||
import { neo4jgraphql } from 'neo4j-graphql-js'
|
import { neo4jgraphql } from 'neo4j-graphql-js'
|
||||||
|
import uuid from 'uuid/v4'
|
||||||
import fileUpload from './fileUpload'
|
import fileUpload from './fileUpload'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -9,36 +10,35 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
CreatePost: async (object, params, context, resolveInfo) => {
|
CreatePost: async (object, params, context, resolveInfo) => {
|
||||||
const { categories } = params
|
const { categoryIds } = params
|
||||||
let post
|
delete params.categoryIds
|
||||||
params = await fileUpload(params, { file: 'imageUpload', url: 'image' })
|
params = await fileUpload(params, { file: 'imageUpload', url: 'image' })
|
||||||
post = await neo4jgraphql(object, params, context, resolveInfo, false)
|
params.id = params.id || uuid()
|
||||||
|
let cypher = `CREATE (post:Post {params})
|
||||||
|
WITH post
|
||||||
|
MATCH (author:User {id: $userId})
|
||||||
|
MERGE (post)<-[:WROTE]-(author)
|
||||||
|
`
|
||||||
|
if (categoryIds) {
|
||||||
|
cypher += `WITH post
|
||||||
|
UNWIND $categoryIds AS categoryId
|
||||||
|
MATCH (category:Category {id: categoryId})
|
||||||
|
MERGE (post)-[:CATEGORIZED]->(category)
|
||||||
|
`
|
||||||
|
}
|
||||||
|
cypher += `RETURN post`
|
||||||
|
const variables = { userId: context.user.id, categoryIds, params }
|
||||||
|
|
||||||
const session = context.driver.session()
|
const session = context.driver.session()
|
||||||
await session.run(
|
const transactionRes = await session.run(cypher, variables)
|
||||||
'MATCH (author:User {id: $userId}), (post:Post {id: $postId}) ' +
|
|
||||||
'MERGE (post)<-[:WROTE]-(author) ' +
|
const [post] = transactionRes.records.map(record => {
|
||||||
'RETURN author',
|
return record.get('post')
|
||||||
{
|
})
|
||||||
userId: context.user.id,
|
|
||||||
postId: post.id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if (categories && categories.length) {
|
|
||||||
await session.run(
|
|
||||||
`MATCH (post:Post {id: $postId})
|
|
||||||
UNWIND $categories AS categoryId
|
|
||||||
MATCH (category:Category {id: categoryId})
|
|
||||||
MERGE (post)-[:CATEGORIZED]->(category)
|
|
||||||
RETURN category`,
|
|
||||||
{
|
|
||||||
categories,
|
|
||||||
postId: post.id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
session.close()
|
session.close()
|
||||||
return post
|
|
||||||
|
return post.properties
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -118,19 +118,16 @@ describe('CreatePost', () => {
|
|||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
const createPostWithCategoriesMutation = `
|
const createPostWithCategoriesMutation = `
|
||||||
mutation($title: String!, $content: String!, $categories: [ID]) {
|
mutation($title: String!, $content: String!, $categoryIds: [ID]) {
|
||||||
CreatePost(title: $title, content: $content, categories: $categories) {
|
CreatePost(title: $title, content: $content, categoryIds: $categoryIds) {
|
||||||
id
|
id
|
||||||
categories {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
const creatPostWithCategoriesVariables = {
|
const creatPostWithCategoriesVariables = {
|
||||||
title: postTitle,
|
title: postTitle,
|
||||||
content: postContent,
|
content: postContent,
|
||||||
categories: ['cat9', 'cat4', 'cat15'],
|
categoryIds: ['cat9', 'cat4', 'cat15'],
|
||||||
}
|
}
|
||||||
const postQueryWithCategories = `
|
const postQueryWithCategories = `
|
||||||
query($id: ID) {
|
query($id: ID) {
|
||||||
|
|||||||
@ -56,7 +56,7 @@ type Mutation {
|
|||||||
createdAt: String
|
createdAt: String
|
||||||
updatedAt: String
|
updatedAt: String
|
||||||
language: String
|
language: String
|
||||||
categories: [ID]
|
categoryIds: [ID]
|
||||||
contentExcerpt: String
|
contentExcerpt: String
|
||||||
): Post
|
): Post
|
||||||
}
|
}
|
||||||
|
|||||||
@ -106,7 +106,7 @@ describe('ContributionForm.vue', () => {
|
|||||||
content: postContent,
|
content: postContent,
|
||||||
language: 'en',
|
language: 'en',
|
||||||
id: null,
|
id: null,
|
||||||
categories: null,
|
categoryIds: null,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
postTitleInput = wrapper.find('.ds-input')
|
postTitleInput = wrapper.find('.ds-input')
|
||||||
@ -132,9 +132,9 @@ describe('ContributionForm.vue', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('supports adding categories', async () => {
|
it('supports adding categories', async () => {
|
||||||
const categories = ['cat12', 'cat15', 'cat37']
|
const categoryIds = ['cat12', 'cat15', 'cat37']
|
||||||
expectedParams.variables.categories = categories
|
expectedParams.variables.categoryIds = categoryIds
|
||||||
wrapper.find(CategoriesSelect).vm.$emit('updateCategories', categories)
|
wrapper.find(CategoriesSelect).vm.$emit('updateCategories', categoryIds)
|
||||||
await wrapper.find('form').trigger('submit')
|
await wrapper.find('form').trigger('submit')
|
||||||
expect(mocks.$apollo.mutate).toHaveBeenCalledWith(expect.objectContaining(expectedParams))
|
expect(mocks.$apollo.mutate).toHaveBeenCalledWith(expect.objectContaining(expectedParams))
|
||||||
})
|
})
|
||||||
@ -215,7 +215,7 @@ describe('ContributionForm.vue', () => {
|
|||||||
content: postContent,
|
content: postContent,
|
||||||
language: propsData.contribution.language,
|
language: propsData.contribution.language,
|
||||||
id: propsData.contribution.id,
|
id: propsData.contribution.id,
|
||||||
categories: null,
|
categoryIds: null,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
postTitleInput = wrapper.find('.ds-input')
|
postTitleInput = wrapper.find('.ds-input')
|
||||||
|
|||||||
@ -68,7 +68,7 @@ export default {
|
|||||||
content: '',
|
content: '',
|
||||||
language: null,
|
language: null,
|
||||||
languageOptions: [],
|
languageOptions: [],
|
||||||
categories: null,
|
categoryIds: null,
|
||||||
},
|
},
|
||||||
formSchema: {
|
formSchema: {
|
||||||
title: { required: true, min: 3, max: 64 },
|
title: { required: true, min: 3, max: 64 },
|
||||||
@ -110,7 +110,7 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
submit() {
|
submit() {
|
||||||
const { title, content, language, categories } = this.form
|
const { title, content, language, categoryIds } = this.form
|
||||||
this.loading = true
|
this.loading = true
|
||||||
this.$apollo
|
this.$apollo
|
||||||
.mutate({
|
.mutate({
|
||||||
@ -120,7 +120,7 @@ export default {
|
|||||||
title,
|
title,
|
||||||
content,
|
content,
|
||||||
language: language ? language.value : this.$i18n.locale(),
|
language: language ? language.value : this.$i18n.locale(),
|
||||||
categories,
|
categoryIds,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
@ -150,7 +150,7 @@ export default {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
updateCategories(ids) {
|
updateCategories(ids) {
|
||||||
this.form.categories = ids
|
this.form.categoryIds = ids
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
apollo: {
|
apollo: {
|
||||||
|
|||||||
@ -3,8 +3,13 @@ import gql from 'graphql-tag'
|
|||||||
export default () => {
|
export default () => {
|
||||||
return {
|
return {
|
||||||
CreatePost: gql`
|
CreatePost: gql`
|
||||||
mutation($title: String!, $content: String!, $language: String, $categories: [ID]) {
|
mutation($title: String!, $content: String!, $language: String, $categoryIds: [ID]) {
|
||||||
CreatePost(title: $title, content: $content, language: $language, categories: $categories) {
|
CreatePost(
|
||||||
|
title: $title
|
||||||
|
content: $content
|
||||||
|
language: $language
|
||||||
|
categoryIds: $categoryIds
|
||||||
|
) {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
slug
|
slug
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user