From dfef4fe05fa74bad1ea8e2c91ffb1681538c3e42 Mon Sep 17 00:00:00 2001 From: Matt Rider Date: Tue, 16 Apr 2019 19:48:59 -0300 Subject: [PATCH] Add custom resolver, update factories --- backend/src/graphql-schema.js | 4 +++- backend/src/resolvers/comments.js | 28 ++++++++++++++++++++++++++ backend/src/resolvers/socialMedia.js | 2 +- backend/src/schema.graphql | 1 + backend/src/seed/factories/comments.js | 12 +++++------ 5 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 backend/src/resolvers/comments.js diff --git a/backend/src/graphql-schema.js b/backend/src/graphql-schema.js index 1e13c95f4..7befd0507 100644 --- a/backend/src/graphql-schema.js +++ b/backend/src/graphql-schema.js @@ -9,6 +9,7 @@ import moderation from './resolvers/moderation.js' import rewards from './resolvers/rewards.js' import socialMedia from './resolvers/socialMedia.js' import notifications from './resolvers/notifications' +import comments from './resolvers/comments' export const typeDefs = fs .readFileSync( @@ -29,6 +30,7 @@ export const resolvers = { ...moderation.Mutation, ...rewards.Mutation, ...socialMedia.Mutation, - ...notifications.Mutation + ...notifications.Mutation, + ...comments.Mutation } } diff --git a/backend/src/resolvers/comments.js b/backend/src/resolvers/comments.js new file mode 100644 index 000000000..74454322d --- /dev/null +++ b/backend/src/resolvers/comments.js @@ -0,0 +1,28 @@ +import { neo4jgraphql } from 'neo4j-graphql-js' + +export default { + Mutation: { + CreateComment: async (object, params, context, resolveInfo) => { + const { postId } = params + + const result = 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: result.id + } + ) + const [comment] = transactionRes.records.map(record => { + return record.get('comment') + }) + + session.close() + + return comment + } + } +} diff --git a/backend/src/resolvers/socialMedia.js b/backend/src/resolvers/socialMedia.js index 3adf0e2d0..310375820 100644 --- a/backend/src/resolvers/socialMedia.js +++ b/backend/src/resolvers/socialMedia.js @@ -3,7 +3,7 @@ import { neo4jgraphql } from 'neo4j-graphql-js' export default { Mutation: { CreateSocialMedia: async (object, params, context, resolveInfo) => { - const socialMedia = await neo4jgraphql(object, params, context, resolveInfo, true) + const socialMedia = await neo4jgraphql(object, params, context, resolveInfo, false) const session = context.driver.session() await session.run( `MATCH (owner:User {id: $userId}), (socialMedia:SocialMedia {id: $socialMediaId}) diff --git a/backend/src/schema.graphql b/backend/src/schema.graphql index 94e28d0d7..6c35e082d 100644 --- a/backend/src/schema.graphql +++ b/backend/src/schema.graphql @@ -27,6 +27,7 @@ type Mutation { enable(id: ID!): ID reward(fromBadgeId: ID!, toUserId: ID!): ID unreward(fromBadgeId: ID!, toUserId: ID!): ID + CreateComment(id: ID!, postId: ID!, content: String!) : Comment "Shout the given Type and ID" shout(id: ID!, type: ShoutTypeEnum): Boolean! @cypher(statement: """ MATCH (n {id: $id})<-[:WROTE]-(wu:User), (u:User {id: $cypherParams.currentUserId}) diff --git a/backend/src/seed/factories/comments.js b/backend/src/seed/factories/comments.js index 92dca5b14..29d0722b7 100644 --- a/backend/src/seed/factories/comments.js +++ b/backend/src/seed/factories/comments.js @@ -4,22 +4,20 @@ import uuid from 'uuid/v4' export default function (params) { const { id = uuid(), + postId = uuid(), content = [ faker.lorem.sentence(), faker.lorem.sentence() - ].join('. '), - disabled = false, - deleted = false + ].join('. ') } = params return ` mutation { CreateComment( id: "${id}", - content: "${content}", - disabled: ${disabled}, - deleted: ${deleted} - ) { id } + postId: "${postId}", + content: "${content}" + ) { id, content } } ` }