diff --git a/backend/src/resolvers/follow.js b/backend/src/resolvers/follow.js index 860ee777d..2a835f636 100644 --- a/backend/src/resolvers/follow.js +++ b/backend/src/resolvers/follow.js @@ -1,49 +1,49 @@ +import gql from 'graphql-tag' +import { neo4jgraphql } from 'neo4j-graphql-js' + export default { - Mutation: { - follow: async (_object, params, context, _resolveInfo) => { - const { followedId, followedType } = params - const session = context.driver.session() - - let sessionRes = await session.run( - `MATCH (n {id: $id}), (u:User {id: $cypherParams.currentUserId}) - WHERE $type IN labels(n) AND NOT $id = $cypherParams.currentUserId - MERGE (u)-[r:FOLLOWS]->(n) - RETURN COUNT(r) > 0`, - { - badgeId: fromBadgeId, - rewardedUserId: toUserId - } - ) - - const [rewardedUser] = sessionRes.records.map(record => { - return record.get('rewardedUser') - }) - - session.close() - - return rewardedUser.id - }, - - unfollow: async (_object, params, context, _resolveInfo) => { - const { fromBadgeId, toUserId } = params - const session = context.driver.session() - - let sessionRes = await session.run( - `MATCH (badge:Badge {id: $badgeId})-[reward:REWARDED]->(rewardedUser:User {id: $rewardedUserId}) - DELETE reward - RETURN rewardedUser {.id}`, - { - badgeId: fromBadgeId, - rewardedUserId: toUserId - } - ) - const [rewardedUser] = sessionRes.records.map(record => { - return record.get('rewardedUser') - }) - session.close() - - return rewardedUser.id - } + Mutation: { + follow: async (object, params, context, resolveInfo) => { + const result = await neo4jgraphql(object, params, context, resolveInfo, true) + const { followedId, followedType } = params + + const session = context.driver.session() + await session.run( + gql` + MATCH (n {id: $followedId}), (u:User {id: $userId}) + WHERE $type IN labels(n) AND NOT $id = $userId + MERGE (u)-[r:FOLLOWS]->(n) + RETURN COUNT(r) > 0`, + { + followedId: followedId, + type: followedType, + userId: context.user.id + } + ) + session.close() + + return result } + + // unfollow: async (_object, params, context, _resolveInfo) => { + // const { fromBadgeId, toUserId } = params + // const session = context.driver.session() + + // let sessionRes = await session.run( + // `MATCH (badge:Badge {id: $badgeId})-[reward:REWARDED]->(rewardedUser:User {id: $rewardedUserId}) + // DELETE reward + // RETURN rewardedUser {.id}`, + // { + // badgeId: fromBadgeId, + // rewardedUserId: toUserId + // } + // ) + // const [rewardedUser] = sessionRes.records.map(record => { + // return record.get('rewardedUser') + // }) + // session.close() + + // return rewardedUser.id + // } } - \ No newline at end of file +} diff --git a/backend/src/schema.graphql b/backend/src/schema.graphql index db468471a..107e134fa 100644 --- a/backend/src/schema.graphql +++ b/backend/src/schema.graphql @@ -42,12 +42,7 @@ type Mutation { RETURN COUNT(r) > 0 """) "Follow the given Type and ID" - follow(id: ID!, type: FollowTypeEnum): Boolean! @cypher(statement: """ - MATCH (n {id: $id}), (u:User {id: $cypherParams.currentUserId}) - WHERE $type IN labels(n) AND NOT $id = $cypherParams.currentUserId - MERGE (u)-[r:FOLLOWS]->(n) - RETURN COUNT(r) > 0 - """) + follow(id: ID!, type: FollowTypeEnum): Boolean! "Unfollow the given Type and ID" unfollow(id: ID!, type: FollowTypeEnum): Boolean! @cypher(statement: """ MATCH (:User {id: $cypherParams.currentUserId})-[r:FOLLOWS]->(n {id: $id})