From 70c72e8f95abbe7623e72cfef6079c780da8afe1 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Mon, 16 Sep 2019 12:35:14 +0200 Subject: [PATCH 1/2] Remove deleted/disabled/createdAt from Comment mutations --- backend/src/schema/resolvers/comments.js | 70 +++++++++++------------ backend/src/schema/types/type/Comment.gql | 5 -- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/backend/src/schema/resolvers/comments.js b/backend/src/schema/resolvers/comments.js index 1f6803e09..e6e6c6155 100644 --- a/backend/src/schema/resolvers/comments.js +++ b/backend/src/schema/resolvers/comments.js @@ -1,4 +1,4 @@ -import { neo4jgraphql } from 'neo4j-graphql-js' +import uuid from 'uuid/v4' import Resolver from './helpers/Resolver' export default { @@ -10,44 +10,44 @@ export default { // because we use relationships for this. So, we are deleting it from params // before comment creation. delete params.postId + params.id = params.id || uuid() + const session = context.driver.session() - const commentWithoutRelationships = await neo4jgraphql( - object, - params, - context, - resolveInfo, - false, - ) - - const transactionRes = await session.run( - ` - MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId}), (author:User {id: $userId}) + const createCommentCypher = ` + MATCH (post:Post {id: $postId}) + MATCH (author:User {id: $userId}) + WITH post, author + CREATE (comment:Comment {params}) + SET comment.createdAt = toString(datetime()) + SET comment.updatedAt = toString(datetime()) MERGE (post)<-[:COMMENTS]-(comment)<-[:WROTE]-(author) - RETURN comment, author`, - { - userId: context.user.id, - postId, - commentId: commentWithoutRelationships.id, - }, - ) - - const [commentWithAuthor] = transactionRes.records.map(record => { - return { - comment: record.get('comment'), - author: record.get('author'), - } + RETURN comment + ` + const transactionRes = await session.run(createCommentCypher, { + userId: context.user.id, + postId, + params, }) - - const { comment, author } = commentWithAuthor - - const commentReturnedWithAuthor = { - ...comment.properties, - author: author.properties, - } session.close() - return commentReturnedWithAuthor + + const [comment] = transactionRes.records.map(record => record.get('comment').properties) + + return comment }, - DeleteComment: async (object, args, context, resolveInfo) => { + UpdateComment: async (_parent, params, context, _resolveInfo) => { + const session = context.driver.session() + const updateCommentCypher = ` + MATCH (comment:Comment {id: $params.id}) + SET comment += $params + SET comment.updatedAt = toString(datetime()) + RETURN comment + ` + const transactionRes = await session.run(updateCommentCypher, { params }) + session.close() + const [comment] = transactionRes.records.map(record => record.get('comment').properties) + return comment + }, + DeleteComment: async (_parent, args, context, _resolveInfo) => { const session = context.driver.session() const transactionRes = await session.run( ` @@ -72,4 +72,4 @@ export default { }, }), }, -} +} \ No newline at end of file diff --git a/backend/src/schema/types/type/Comment.gql b/backend/src/schema/types/type/Comment.gql index 4abebcba6..1ccf617ef 100644 --- a/backend/src/schema/types/type/Comment.gql +++ b/backend/src/schema/types/type/Comment.gql @@ -18,16 +18,11 @@ type Mutation { postId: ID! content: String! contentExcerpt: String - deleted: Boolean - disabled: Boolean - createdAt: String ): Comment UpdateComment( id: ID! content: String! contentExcerpt: String - deleted: Boolean - disabled: Boolean ): Comment DeleteComment(id: ID!): Comment } From f66d234e854e4e70eb8543787d41f5c6c940cf5c Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Mon, 16 Sep 2019 13:41:10 +0200 Subject: [PATCH 2/2] Fix lint --- backend/src/schema/resolvers/comments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/schema/resolvers/comments.js b/backend/src/schema/resolvers/comments.js index e6e6c6155..7378238bb 100644 --- a/backend/src/schema/resolvers/comments.js +++ b/backend/src/schema/resolvers/comments.js @@ -72,4 +72,4 @@ export default { }, }), }, -} \ No newline at end of file +}