Finish refactoring posts resolver

This commit is contained in:
mattwr18 2019-12-06 18:56:20 +01:00
parent 6ed435364c
commit 3bc944b06d

View File

@ -124,8 +124,8 @@ export default {
return createPostTransactionResponse.records.map(record => record.get('post').properties) return createPostTransactionResponse.records.map(record => record.get('post').properties)
}) })
try { try {
const [posts] = await writeTxResultPromise const [post] = await writeTxResultPromise
return posts return post
} catch (e) { } catch (e) {
if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed') if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed')
throw new UserInputError('Post with this slug already exists!') throw new UserInputError('Post with this slug already exists!')
@ -184,23 +184,25 @@ export default {
DeletePost: async (object, args, context, resolveInfo) => { DeletePost: async (object, args, context, resolveInfo) => {
const session = context.driver.session() const session = context.driver.session()
try { const writeTxResultPromise = session.writeTransaction(async transaction => {
// we cannot set slug to 'UNAVAILABE' because of unique constraints const deletePostTransactionResponse = await transaction.run(
const transactionRes = await session.run(
` `
MATCH (post:Post {id: $postId}) MATCH (post:Post {id: $postId})
OPTIONAL MATCH (post)<-[:COMMENTS]-(comment:Comment) OPTIONAL MATCH (post)<-[:COMMENTS]-(comment:Comment)
SET post.deleted = TRUE SET post.deleted = TRUE
SET post.content = 'UNAVAILABLE' SET post.content = 'UNAVAILABLE'
SET post.contentExcerpt = 'UNAVAILABLE' SET post.contentExcerpt = 'UNAVAILABLE'
SET post.title = 'UNAVAILABLE' SET post.title = 'UNAVAILABLE'
SET comment.deleted = TRUE SET comment.deleted = TRUE
REMOVE post.image REMOVE post.image
RETURN post RETURN post
`, `,
{ postId: args.id }, { postId: args.id },
) )
const [post] = transactionRes.records.map(record => record.get('post').properties) return deletePostTransactionResponse.records.map(record => record.get('post').properties)
})
try {
const [post] = await writeTxResultPromise
return post return post
} finally { } finally {
session.close() session.close()
@ -210,21 +212,24 @@ export default {
const { to, data } = params const { to, data } = params
const { user } = context const { user } = context
const session = context.driver.session() const session = context.driver.session()
try { const writeTxResultPromise = session.writeTransaction(async transaction => {
const transactionRes = await session.run( const addPostEmotionsTransactionResponse = await transaction.run(
`MATCH (userFrom:User {id: $user.id}), (postTo:Post {id: $to.id}) `
MERGE (userFrom)-[emotedRelation:EMOTED {emotion: $data.emotion}]->(postTo) MATCH (userFrom:User {id: $user.id}), (postTo:Post {id: $to.id})
RETURN userFrom, postTo, emotedRelation`, MERGE (userFrom)-[emotedRelation:EMOTED {emotion: $data.emotion}]->(postTo)
RETURN userFrom, postTo, emotedRelation`,
{ user, to, data }, { user, to, data },
) )
return addPostEmotionsTransactionResponse.records.map(record => {
const [emoted] = transactionRes.records.map(record => {
return { return {
from: { ...record.get('userFrom').properties }, from: { ...record.get('userFrom').properties },
to: { ...record.get('postTo').properties }, to: { ...record.get('postTo').properties },
...record.get('emotedRelation').properties, ...record.get('emotedRelation').properties,
} }
}) })
})
try {
const [emoted] = await writeTxResultPromise
return emoted return emoted
} finally { } finally {
session.close() session.close()
@ -234,20 +239,25 @@ export default {
const { to, data } = params const { to, data } = params
const { id: from } = context.user const { id: from } = context.user
const session = context.driver.session() const session = context.driver.session()
try { const writeTxResultPromise = session.writeTransaction(async transaction => {
const transactionRes = await session.run( const removePostEmotionsTransactionResponse = await transaction.run(
`MATCH (userFrom:User {id: $from})-[emotedRelation:EMOTED {emotion: $data.emotion}]->(postTo:Post {id: $to.id}) `
DELETE emotedRelation MATCH (userFrom:User {id: $from})-[emotedRelation:EMOTED {emotion: $data.emotion}]->(postTo:Post {id: $to.id})
RETURN userFrom, postTo`, DELETE emotedRelation
RETURN userFrom, postTo
`,
{ from, to, data }, { from, to, data },
) )
const [emoted] = transactionRes.records.map(record => { return removePostEmotionsTransactionResponse.records.map(record => {
return { return {
from: { ...record.get('userFrom').properties }, from: { ...record.get('userFrom').properties },
to: { ...record.get('postTo').properties }, to: { ...record.get('postTo').properties },
emotion: data.emotion, emotion: data.emotion,
} }
}) })
})
try {
const [emoted] = await writeTxResultPromise
return emoted return emoted
} finally { } finally {
session.close() session.close()
@ -351,21 +361,28 @@ export default {
relatedContributions: async (parent, params, context, resolveInfo) => { relatedContributions: async (parent, params, context, resolveInfo) => {
if (typeof parent.relatedContributions !== 'undefined') return parent.relatedContributions if (typeof parent.relatedContributions !== 'undefined') return parent.relatedContributions
const { id } = parent const { id } = parent
const statement = `
MATCH (p:Post {id: $id})-[:TAGGED|CATEGORIZED]->(categoryOrTag)<-[:TAGGED|CATEGORIZED]-(post:Post)
WHERE NOT post.deleted AND NOT post.disabled
RETURN DISTINCT post
LIMIT 10
`
let relatedContributions
const session = context.driver.session() const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async transaction => {
const relatedContributionsTransactionResponse = await transaction.run(
`
MATCH (p:Post {id: $id})-[:TAGGED|CATEGORIZED]->(categoryOrTag)<-[:TAGGED|CATEGORIZED]-(post:Post)
WHERE NOT post.deleted AND NOT post.disabled
RETURN DISTINCT post
LIMIT 10
`,
{ id },
)
return relatedContributionsTransactionResponse.records.map(
record => record.get('post').properties,
)
})
try { try {
const result = await session.run(statement, { id }) const relatedContributions = await writeTxResultPromise
relatedContributions = result.records.map(r => r.get('post').properties) return relatedContributions
} finally { } finally {
session.close() session.close()
} }
return relatedContributions
}, },
}, },
} }