Refactor resolver, rename params

- Use one cypher statement

Co-authored-by: Tirokk <wolle.huss@pjannto.com>
This commit is contained in:
Matt Rider 2019-06-28 10:17:22 -03:00
parent 351f258fab
commit 865e2c0ec3
6 changed files with 46 additions and 44 deletions

View File

@ -1,4 +1,5 @@
import { neo4jgraphql } from 'neo4j-graphql-js'
import uuid from 'uuid/v4'
import fileUpload from './fileUpload'
export default {
@ -9,36 +10,35 @@ export default {
},
CreatePost: async (object, params, context, resolveInfo) => {
const { categories } = params
let post
const { categoryIds } = params
delete params.categoryIds
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()
await session.run(
'MATCH (author:User {id: $userId}), (post:Post {id: $postId}) ' +
'MERGE (post)<-[:WROTE]-(author) ' +
'RETURN author',
{
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,
},
)
}
const transactionRes = await session.run(cypher, variables)
const [post] = transactionRes.records.map(record => {
return record.get('post')
})
session.close()
return post
return post.properties
},
},
}

View File

@ -118,19 +118,16 @@ describe('CreatePost', () => {
}),
])
const createPostWithCategoriesMutation = `
mutation($title: String!, $content: String!, $categories: [ID]) {
CreatePost(title: $title, content: $content, categories: $categories) {
mutation($title: String!, $content: String!, $categoryIds: [ID]) {
CreatePost(title: $title, content: $content, categoryIds: $categoryIds) {
id
categories {
id
}
}
}
`
const creatPostWithCategoriesVariables = {
title: postTitle,
content: postContent,
categories: ['cat9', 'cat4', 'cat15'],
categoryIds: ['cat9', 'cat4', 'cat15'],
}
const postQueryWithCategories = `
query($id: ID) {

View File

@ -56,7 +56,7 @@ type Mutation {
createdAt: String
updatedAt: String
language: String
categories: [ID]
categoryIds: [ID]
contentExcerpt: String
): Post
}

View File

@ -106,7 +106,7 @@ describe('ContributionForm.vue', () => {
content: postContent,
language: 'en',
id: null,
categories: null,
categoryIds: null,
},
}
postTitleInput = wrapper.find('.ds-input')
@ -132,9 +132,9 @@ describe('ContributionForm.vue', () => {
})
it('supports adding categories', async () => {
const categories = ['cat12', 'cat15', 'cat37']
expectedParams.variables.categories = categories
wrapper.find(CategoriesSelect).vm.$emit('updateCategories', categories)
const categoryIds = ['cat12', 'cat15', 'cat37']
expectedParams.variables.categoryIds = categoryIds
wrapper.find(CategoriesSelect).vm.$emit('updateCategories', categoryIds)
await wrapper.find('form').trigger('submit')
expect(mocks.$apollo.mutate).toHaveBeenCalledWith(expect.objectContaining(expectedParams))
})
@ -215,7 +215,7 @@ describe('ContributionForm.vue', () => {
content: postContent,
language: propsData.contribution.language,
id: propsData.contribution.id,
categories: null,
categoryIds: null,
},
}
postTitleInput = wrapper.find('.ds-input')

View File

@ -68,7 +68,7 @@ export default {
content: '',
language: null,
languageOptions: [],
categories: null,
categoryIds: null,
},
formSchema: {
title: { required: true, min: 3, max: 64 },
@ -110,7 +110,7 @@ export default {
},
methods: {
submit() {
const { title, content, language, categories } = this.form
const { title, content, language, categoryIds } = this.form
this.loading = true
this.$apollo
.mutate({
@ -120,7 +120,7 @@ export default {
title,
content,
language: language ? language.value : this.$i18n.locale(),
categories,
categoryIds,
},
})
.then(res => {
@ -150,7 +150,7 @@ export default {
})
},
updateCategories(ids) {
this.form.categories = ids
this.form.categoryIds = ids
},
},
apollo: {

View File

@ -3,8 +3,13 @@ import gql from 'graphql-tag'
export default () => {
return {
CreatePost: gql`
mutation($title: String!, $content: String!, $language: String, $categories: [ID]) {
CreatePost(title: $title, content: $content, language: $language, categories: $categories) {
mutation($title: String!, $content: String!, $language: String, $categoryIds: [ID]) {
CreatePost(
title: $title
content: $content
language: $language
categoryIds: $categoryIds
) {
id
title
slug