Notifications resolver capable of orderBy + filter

This commit is contained in:
roschaefer 2019-08-29 19:12:20 +02:00
parent 643d175ef6
commit 4b96454b90
10 changed files with 57 additions and 65 deletions

View File

@ -12,11 +12,9 @@ export default {
CreatePost: setCreatedAt,
CreateComment: setCreatedAt,
CreateOrganization: setCreatedAt,
CreateNotification: setCreatedAt,
UpdateUser: setUpdatedAt,
UpdatePost: setUpdatedAt,
UpdateComment: setUpdatedAt,
UpdateOrganization: setUpdatedAt,
UpdateNotification: setUpdatedAt,
},
}

View File

@ -149,7 +149,6 @@ const permissions = shield(
Category: allow,
Tag: allow,
Report: isModerator,
Notification: isAdmin,
statistics: allow,
currentUser: allow,
Post: or(onlyEnabledContent, isModerator),
@ -169,7 +168,6 @@ const permissions = shield(
Signup: isAdmin,
SignupVerification: allow,
CreateInvitationCode: and(isAuthenticated, or(not(invitationLimitReached), isAdmin)),
UpdateNotification: belongsToMe,
UpdateUser: onlyYourself,
CreatePost: isAuthenticated,
UpdatePost: isAuthor,
@ -199,6 +197,7 @@ const permissions = shield(
RemovePostEmotions: isAuthenticated,
block: isAuthenticated,
unblock: isAuthenticated,
markAsRead: belongsToMe
},
User: {
email: isMyOwn,

View File

@ -20,6 +20,7 @@ export default applyScalars(
'Statistics',
'LoggedInUser',
'SocialMedia',
'NOTIFIED',
],
// add 'User' here as soon as possible
},
@ -32,6 +33,7 @@ export default applyScalars(
'Statistics',
'LoggedInUser',
'SocialMedia',
'NOTIFIED',
],
// add 'User' here as soon as possible
},

View File

@ -2,15 +2,40 @@ import { neo4jgraphql } from 'neo4j-graphql-js'
export default {
Query: {
notifications: async (parent, params, context, resolveInfo) => {
notifications: async (parent, args, context, resolveInfo) => {
const { user, driver } = context
let session
let notifications
let whereClause
let orderByClause
switch(args.read) {
case true:
whereClause = 'WHERE notification.read = TRUE'
break;
case false:
whereClause = 'WHERE notification.read = FALSE'
break;
default:
whereClause = ''
}
switch(args.orderBy) {
case 'createdAt_asc':
orderByClause = 'ORDER BY notification.createdAt ASC'
break;
case 'createdAt_desc':
orderByClause = 'ORDER BY notification.createdAt DESC'
break;
default:
orderByClause = ''
}
try {
session = context.driver.session()
const cypher = `
MATCH (resource)-[notification:NOTIFIED]->(user:User {id:$id})
${whereClause}
RETURN resource, notification, user
${orderByClause}
`
const result = await session.run(cypher, { id: user.id })
const resourceTypes = ['Post', 'Comment']
@ -32,13 +57,10 @@ export default {
}
return notifications
},
Notification: (object, params, context, resolveInfo) => {
return neo4jgraphql(object, params, context, resolveInfo, false)
},
},
Mutation: {
UpdateNotification: (object, params, context, resolveInfo) => {
return neo4jgraphql(object, params, context, resolveInfo, false)
},
markAsRead: async (parent, params, context, resolveInfo) => {
return null
}
},
}

View File

@ -26,10 +26,10 @@ afterEach(async () => {
await factory.cleanDatabase()
})
describe('Notification', () => {
describe('notifications', () => {
const notificationQuery = gql`
query {
Notification {
notifications {
id
}
}
@ -41,10 +41,6 @@ describe('Notification', () => {
await expect(client.request(notificationQuery)).rejects.toThrow('Not Authorised')
})
})
})
describe('currentUser notifications', () => {
const variables = {}
describe('authenticated', () => {
let headers
@ -160,15 +156,13 @@ describe('currentUser notifications', () => {
describe('filter for read: false', () => {
const queryCurrentUserNotificationsFilterRead = gql`
query($read: Boolean) {
currentUser {
notifications(read: $read, orderBy: createdAt_desc) {
notifications(read: $read, orderBy: createdAt_desc) {
id
post {
id
}
comment {
id
post {
id
}
comment {
id
}
}
}
}
@ -204,15 +198,13 @@ describe('currentUser notifications', () => {
describe('no filters', () => {
const queryCurrentUserNotifications = gql`
query {
currentUser {
notifications(orderBy: createdAt_desc) {
notifications(orderBy: createdAt_desc) {
id
post {
id
}
comment {
id
post {
id
}
comment {
id
}
}
}
}

View File

@ -7,6 +7,15 @@ type NOTIFIED {
union NotificationSource = Post | Comment
type Query {
notifications: [NOTIFIED]
enum NOTIFIEDOrdering {
createdAt_asc
createdAt_desc
}
type Query {
notifications(read: Boolean, orderBy: NOTIFIEDOrdering): [NOTIFIED]
}
type Mutation {
markAsRead(id: ID!): NOTIFIED
}

View File

@ -1,9 +0,0 @@
type Notification {
id: ID!
read: Boolean
reason: ReasonNotification
createdAt: String
user: User @relation(name: "NOTIFIED", direction: "OUT")
post: Post @relation(name: "NOTIFIED", direction: "IN")
comment: Comment @relation(name: "NOTIFIED", direction: "IN")
}

View File

@ -24,8 +24,6 @@ type User {
createdAt: String
updatedAt: String
notifications(read: Boolean): [Notification]! @relation(name: "NOTIFIED", direction: "IN")
friends: [User]! @relation(name: "FRIENDS", direction: "BOTH")
friendsCount: Int! @cypher(statement: "MATCH (this)<-[:FRIENDS]->(r:User) RETURN COUNT(DISTINCT r)")

View File

@ -8,7 +8,6 @@ import createComment from './comments.js'
import createCategory from './categories.js'
import createTag from './tags.js'
import createReport from './reports.js'
import createNotification from './notifications.js'
export const seedServerHost = 'http://127.0.0.1:4001'
@ -31,7 +30,6 @@ const factories = {
Category: createCategory,
Tag: createTag,
Report: createReport,
Notification: createNotification,
}
export const cleanDatabase = async (options = {}) => {

View File

@ -1,17 +0,0 @@
import uuid from 'uuid/v4'
export default function(params) {
const { id = uuid(), read = false } = params
return {
mutation: `
mutation($id: ID, $read: Boolean) {
CreateNotification(id: $id, read: $read) {
id
read
}
}
`,
variables: { id, read },
}
}