Add Cypher statement for ordering

- minor db manipulation text change
This commit is contained in:
Wolfgang Huß 2019-10-16 13:32:45 +02:00
parent 452bf4127c
commit 30f268525f
2 changed files with 58 additions and 43 deletions

View File

@ -1,8 +1,9 @@
export default {
Mutation: {
report: async (_parent, params, { driver, user }, _resolveInfo) => {
report: async (_parent, params, context, _resolveInfo) => {
let createdRelationshipWithNestedAttributes
const { resourceId, reasonCategory, reasonDescription } = params
const { driver, user } = context
const session = driver.session()
const writeTxResultPromise = session.writeTransaction(async txc => {
const reportRelationshipTransactionResponse = await txc.run(
@ -58,19 +59,30 @@ export default {
},
},
Query: {
reports: async (_parent, _params, { driver }, _resolveInfo) => {
reports: async (_parent, params, context, _resolveInfo) => {
const { driver } = context
const session = driver.session()
const res = await session.run(
`
let response
let orderByClause
switch (params.orderBy) {
// case 'createdAt_asc':
// orderByClause = 'ORDER BY report.createdAt ASC'
// break
case 'createdAt_desc':
orderByClause = 'ORDER BY report.createdAt DESC'
break
default:
orderByClause = ''
}
try {
const cypher = `
MATCH (submitter:User)-[report:REPORTED]->(resource)
WHERE resource:User OR resource:Comment OR resource:Post
RETURN report, submitter, resource, labels(resource)[0] as type
`,
{},
)
session.close()
const dbResponse = res.records.map(r => {
${orderByClause}
`
const result = await session.run(cypher, {})
const dbResponse = result.records.map(r => {
return {
report: r.get('report'),
submitter: r.get('submitter'),
@ -80,7 +92,7 @@ export default {
})
if (!dbResponse) return null
const response = []
response = []
dbResponse.forEach(ele => {
const { report, submitter, resource, type } = ele
@ -106,6 +118,9 @@ export default {
}
response.push(responseEle)
})
} finally {
session.close()
}
return response
},

View File

@ -21,6 +21,6 @@ DETACH DELETE report
CREATE (submitter)-[reported:REPORTED]->(resource)
SET reported.createdAt = toString(datetime())
SET reported.reasonCategory = 'other'
SET reported.reasonDescription = '!!! Created automatically to ensure database consistency! createdAt is when the database manipulation happened.'
SET reported.reasonDescription = '!!! Created automatically to ensure database consistency! Creation date is when the database manipulation happened.'
RETURN reported;
" | cypher-shell