From 865e2c0ec368d0a00fb7c3bbc2ca2bdcbd2d0b74 Mon Sep 17 00:00:00 2001 From: Matt Rider Date: Fri, 28 Jun 2019 10:17:22 -0300 Subject: [PATCH] Refactor resolver, rename params - Use one cypher statement Co-authored-by: Tirokk --- backend/src/schema/resolvers/posts.js | 52 +++++++++---------- backend/src/schema/resolvers/posts.spec.js | 9 ++-- backend/src/schema/types/schema.gql | 2 +- .../ContributionForm/ContributionForm.spec.js | 10 ++-- webapp/components/ContributionForm/index.vue | 8 +-- webapp/graphql/PostMutations.js | 9 +++- 6 files changed, 46 insertions(+), 44 deletions(-) diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index a9617d255..a19e813ad 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -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 }, }, } diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index 06a342c1c..def99d8bb 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -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) { diff --git a/backend/src/schema/types/schema.gql b/backend/src/schema/types/schema.gql index 3955da8a2..bc3daad26 100644 --- a/backend/src/schema/types/schema.gql +++ b/backend/src/schema/types/schema.gql @@ -56,7 +56,7 @@ type Mutation { createdAt: String updatedAt: String language: String - categories: [ID] + categoryIds: [ID] contentExcerpt: String ): Post } diff --git a/webapp/components/ContributionForm/ContributionForm.spec.js b/webapp/components/ContributionForm/ContributionForm.spec.js index 0991a3ea2..a282ea085 100644 --- a/webapp/components/ContributionForm/ContributionForm.spec.js +++ b/webapp/components/ContributionForm/ContributionForm.spec.js @@ -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') diff --git a/webapp/components/ContributionForm/index.vue b/webapp/components/ContributionForm/index.vue index 670960df5..0b09eee70 100644 --- a/webapp/components/ContributionForm/index.vue +++ b/webapp/components/ContributionForm/index.vue @@ -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: { diff --git a/webapp/graphql/PostMutations.js b/webapp/graphql/PostMutations.js index 4f195eb99..63fccda4a 100644 --- a/webapp/graphql/PostMutations.js +++ b/webapp/graphql/PostMutations.js @@ -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