diff --git a/backend/src/resolvers/comments.js b/backend/src/resolvers/comments.js index 4542c075a..5709b52e1 100644 --- a/backend/src/resolvers/comments.js +++ b/backend/src/resolvers/comments.js @@ -2,11 +2,24 @@ import { neo4jgraphql } from 'neo4j-graphql-js' export default { Mutation: { - CreateComment: (object, params, context, resolveInfo) => { - return neo4jgraphql(object, params, context, resolveInfo, false) - }, - AddPostComments: (object, params, context, resolveInfo) => { - return neo4jgraphql(object, params, context, resolveInfo, false) + CreateComment: async (object, params, context, resolveInfo) => { + const { postId } = params + delete params.postId + const comment = await neo4jgraphql(object, params, context, resolveInfo, true) + + const session = context.driver.session() + + const transactionRes = await session.run(` + MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId}) + MERGE (post)<-[:COMMENTS]-(comment) + RETURN comment {.id, .content}`, { + postId, + commentId: comment.id + } + ) + session.close() + + return comment } } } diff --git a/backend/src/resolvers/comments.spec.js b/backend/src/resolvers/comments.spec.js index a70dfd974..9918038a7 100644 --- a/backend/src/resolvers/comments.spec.js +++ b/backend/src/resolvers/comments.spec.js @@ -19,8 +19,8 @@ afterEach(async () => { describe('CreateComment', () => { const mutation = ` - mutation($id: ID!, $content: String!) { - CreateComment(id: $id, content: $content) { + mutation($postId: ID, $content: String!) { + CreateComment(postId: $postId, content: $content) { id content } @@ -29,7 +29,7 @@ describe('CreateComment', () => { describe('unauthenticated', () => { it('throws authorization error', async () => { variables = { - id: 'c1', + postId: 'p1', content: 'I\'m not authorised to comment' } client = new GraphQLClient(host) @@ -46,12 +46,11 @@ describe('CreateComment', () => { it('creates a post', async () => { variables = { - id: 'c1', + postId: 'p1', content: 'I\'m authorised to comment' } const expected = { CreateComment: { - id: 'c1', content: 'I\'m authorised to comment' } } diff --git a/backend/src/schema.graphql b/backend/src/schema.graphql index ff8b04dfc..99bcd4533 100644 --- a/backend/src/schema.graphql +++ b/backend/src/schema.graphql @@ -210,6 +210,7 @@ type Post { type Comment { id: ID! activityId: String + postId: ID author: User @relation(name: "WROTE", direction: "IN") content: String! contentExcerpt: String