Add relationship for pinned posts and user

- The CreateUser mutation now returns the user who pinned a post and so we can see the user who pinned the post
This commit is contained in:
aonomike 2019-10-09 13:04:35 +03:00 committed by mattwr18
parent 774581f2e0
commit ab06e8a91f
3 changed files with 30 additions and 9 deletions

View File

@ -75,20 +75,29 @@ export default {
},
Mutation: {
CreatePost: async (_parent, params, context, _resolveInfo) => {
const { categoryIds } = params
const { categoryIds, pinned } = params
delete params.pinned
delete params.categoryIds
params = await fileUpload(params, { file: 'imageUpload', url: 'image' })
params.id = params.id || uuid()
let post
const createPostCypher = `CREATE (post:Post {params})
let createPostCypher = `CREATE (post:Post {params})
SET post.createdAt = toString(datetime())
SET post.updatedAt = toString(datetime())
WITH post
MATCH (author:User {id: $userId})
MERGE (post)<-[:WROTE]-(author)
WITH post
UNWIND $categoryIds AS categoryId
WITH post, author`
if (pinned) {
createPostCypher += `
MERGE (post)<-[:PINNED]-(author)
WITH post
`
}
createPostCypher += `UNWIND $categoryIds AS categoryId
MATCH (category:Category {id: categoryId})
MERGE (post)-[:CATEGORIZED]->(category)
RETURN post`
@ -98,7 +107,9 @@ export default {
const session = context.driver.session()
try {
const transactionRes = await session.run(createPostCypher, createPostVariables)
const posts = transactionRes.records.map(record => record.get('post').properties)
const posts = transactionRes.records.map(record => {
return record.get('post').properties
})
post = posts[0]
} catch (e) {
if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed')
@ -225,6 +236,7 @@ export default {
hasOne: {
author: '<-[:WROTE]-(related:User)',
disabledBy: '<-[:DISABLED]-(related:User)',
pinnedBy: '<-[:PINNED]-(related:User)',
},
count: {
commentsCount:

View File

@ -39,7 +39,11 @@ const createPostMutation = gql`
slug
disabled
deleted
pinned
pinnedBy {
id
name
role
}
language
author {
name
@ -422,9 +426,14 @@ describe('CreatePost', () => {
author: {
name: 'Admin',
},
pinned: true,
pinnedBy: {
id: 'current-user',
name: 'Admin',
role: 'admin',
},
},
},
errors: undefined,
}
await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject(

View File

@ -16,7 +16,7 @@ type Post {
createdAt: String
updatedAt: String
language: String
pinned: Boolean
pinnedBy: User @relation(name:"PINNED", direction: "IN")
relatedContributions: [Post]!
@cypher(
statement: """
@ -41,7 +41,7 @@ type Post {
@cypher(
statement: "MATCH (this)<-[:SHOUTED]-(r:User) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)"
)
# Has the currently logged in user shouted that post?
shoutedByCurrentUser: Boolean!
@cypher(