Start refactoring follow

This commit is contained in:
Wolfgang Huß 2019-04-10 11:15:45 +02:00
parent c513b7ba2e
commit 587025e4d3
2 changed files with 47 additions and 52 deletions

View File

@ -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
// }
}
}

View File

@ -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})