From 9b2c707aa3da1bea20aece372e8dfa8774efa7ce Mon Sep 17 00:00:00 2001 From: roschaefer Date: Thu, 29 Aug 2019 22:22:48 +0200 Subject: [PATCH] DRY notifications resolver --- backend/src/schema/resolvers/notifications.js | 45 +++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index 2db62e444..2385c4b1a 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -1,3 +1,18 @@ +const resourceTypes = ['Post', 'Comment'] + +const transformReturnType = record => { + return { + ...record.get('notification').properties, + from: { + __typename: record.get('resource').labels.find(l => resourceTypes.includes(l)), + ...record.get('resource').properties, + }, + to: { + ...record.get('user').properties, + }, + } +} + export default { Query: { notifications: async (parent, args, context, resolveInfo) => { @@ -35,20 +50,7 @@ export default { ${orderByClause} ` const result = await session.run(cypher, { id: user.id }) - const resourceTypes = ['Post', 'Comment'] - notifications = await result.records.map(record => { - return { - ...record.get('notification').properties, - from: { - __typename: record.get('resource').labels.find(l => resourceTypes.includes(l)), - ...record.get('resource').properties, - }, - to: { - __typename: 'User', - ...record.get('user').properties, - }, - } - }) + notifications = await result.records.map(transformReturnType) } finally { session.close() } @@ -67,20 +69,7 @@ export default { RETURN resource, notification, user ` const result = await session.run(cypher, { resourceId: args.id, id: user.id }) - const resourceTypes = ['Post', 'Comment'] - const notifications = await result.records.map(record => { - return { - ...record.get('notification').properties, - from: { - __typename: record.get('resource').labels.find(l => resourceTypes.includes(l)), - ...record.get('resource').properties, - }, - to: { - __typename: 'User', - ...record.get('user').properties, - }, - } - }) + const notifications = await result.records.map(transformReturnType) notification = notifications[0] } finally { session.close()