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 { export default {
Mutation: { Mutation: {
follow: async (_object, params, context, _resolveInfo) => { follow: async (object, params, context, resolveInfo) => {
const { followedId, followedType } = params const result = await neo4jgraphql(object, params, context, resolveInfo, true)
const session = context.driver.session() const { followedId, followedType } = params
let sessionRes = await session.run( const session = context.driver.session()
`MATCH (n {id: $id}), (u:User {id: $cypherParams.currentUserId}) await session.run(
WHERE $type IN labels(n) AND NOT $id = $cypherParams.currentUserId gql`
MERGE (u)-[r:FOLLOWS]->(n) MATCH (n {id: $followedId}), (u:User {id: $userId})
RETURN COUNT(r) > 0`, WHERE $type IN labels(n) AND NOT $id = $userId
{ MERGE (u)-[r:FOLLOWS]->(n)
badgeId: fromBadgeId, RETURN COUNT(r) > 0`,
rewardedUserId: toUserId {
} followedId: followedId,
) type: followedType,
userId: context.user.id
}
)
session.close()
const [rewardedUser] = sessionRes.records.map(record => { return result
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
}
} }
}
// 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 RETURN COUNT(r) > 0
""") """)
"Follow the given Type and ID" "Follow the given Type and ID"
follow(id: ID!, type: FollowTypeEnum): Boolean! @cypher(statement: """ follow(id: ID!, type: FollowTypeEnum): Boolean!
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
""")
"Unfollow the given Type and ID" "Unfollow the given Type and ID"
unfollow(id: ID!, type: FollowTypeEnum): Boolean! @cypher(statement: """ unfollow(id: ID!, type: FollowTypeEnum): Boolean! @cypher(statement: """
MATCH (:User {id: $cypherParams.currentUserId})-[r:FOLLOWS]->(n {id: $id}) MATCH (:User {id: $cypherParams.currentUserId})-[r:FOLLOWS]->(n {id: $id})