Refactored mutation "follow"

Done by @Tirokk and @mattwr18 .
This commit is contained in:
Wolfgang Huß 2019-04-10 14:11:13 +02:00
parent 61c781c922
commit 6a01146647
2 changed files with 19 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import statistics from './resolvers/statistics.js'
import reports from './resolvers/reports.js' import reports from './resolvers/reports.js'
import posts from './resolvers/posts.js' import posts from './resolvers/posts.js'
import moderation from './resolvers/moderation.js' import moderation from './resolvers/moderation.js'
import follow from './resolvers/follow.js'
import rewards from './resolvers/rewards.js' import rewards from './resolvers/rewards.js'
export const typeDefs = fs export const typeDefs = fs
@ -24,6 +25,7 @@ export const resolvers = {
...reports.Mutation, ...reports.Mutation,
...posts.Mutation, ...posts.Mutation,
...moderation.Mutation, ...moderation.Mutation,
...follow.Mutation,
...rewards.Mutation ...rewards.Mutation
} }
} }

View File

@ -1,28 +1,32 @@
import gql from 'graphql-tag' // import gql from 'graphql-tag'
import { neo4jgraphql } from 'neo4j-graphql-js' // 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 result = await neo4jgraphql(object, params, context, resolveInfo, true) // const result = await neo4jgraphql(object, params, context, resolveInfo, true)
const { followedId, followedType } = params const { id, type } = params
const session = context.driver.session() const session = context.driver.session()
await session.run( let sessionRes = await session.run(
gql` `MATCH (node {id: $id}), (user:User {id: $userId})
MATCH (n {id: $followedId}), (u:User {id: $userId}) WHERE $type IN labels(node) AND NOT $id = $userId
WHERE $type IN labels(n) AND NOT $id = $userId MERGE (user)-[relation:FOLLOWS]->(node)
MERGE (u)-[r:FOLLOWS]->(n) RETURN COUNT(relation) > 0 as isFollowed`,
RETURN COUNT(r) > 0`,
{ {
followedId: followedId, id,
type: followedType, type,
userId: context.user.id userId: context.user.id
} }
) )
const [ isFollowed ] = sessionRes.records.map(record => {
return record.get('isFollowed')
})
session.close() session.close()
return result return isFollowed
} }
// unfollow: async (_object, params, context, _resolveInfo) => { // unfollow: async (_object, params, context, _resolveInfo) => {