mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Refactor resolver/spec
- favor transaction functions - add errors undefined, clean up specs
This commit is contained in:
parent
0e757cf94f
commit
6f4ee5f3b7
@ -5,6 +5,7 @@ export default {
|
||||
Mutation: {
|
||||
CreateComment: async (object, params, context, resolveInfo) => {
|
||||
const { postId } = params
|
||||
const { user, driver } = context
|
||||
// Adding relationship from comment to post by passing in the postId,
|
||||
// but we do not want to create the comment with postId as an attribute
|
||||
// because we use relationships for this. So, we are deleting it from params
|
||||
@ -12,26 +13,28 @@ export default {
|
||||
delete params.postId
|
||||
params.id = params.id || uuid()
|
||||
|
||||
const session = context.driver.session()
|
||||
const session = driver.session()
|
||||
|
||||
const writeTxResultPromise = session.writeTransaction(async transaction => {
|
||||
const createCommentTransactionResponse = await transaction.run(
|
||||
`
|
||||
MATCH (post:Post {id: $postId})
|
||||
MATCH (author:User {id: $userId})
|
||||
WITH post, author
|
||||
CREATE (comment:Comment {params})
|
||||
SET comment.createdAt = toString(datetime())
|
||||
SET comment.updatedAt = toString(datetime())
|
||||
MERGE (post)<-[:COMMENTS]-(comment)<-[:WROTE]-(author)
|
||||
RETURN comment
|
||||
`,
|
||||
{ userId: user.id, postId, params },
|
||||
)
|
||||
return createCommentTransactionResponse.records.map(
|
||||
record => record.get('comment').properties,
|
||||
)
|
||||
})
|
||||
try {
|
||||
const createCommentCypher = `
|
||||
MATCH (post:Post {id: $postId})
|
||||
MATCH (author:User {id: $userId})
|
||||
WITH post, author
|
||||
CREATE (comment:Comment {params})
|
||||
SET comment.createdAt = toString(datetime())
|
||||
SET comment.updatedAt = toString(datetime())
|
||||
MERGE (post)<-[:COMMENTS]-(comment)<-[:WROTE]-(author)
|
||||
RETURN comment
|
||||
`
|
||||
const transactionRes = await session.run(createCommentCypher, {
|
||||
userId: context.user.id,
|
||||
postId,
|
||||
params,
|
||||
})
|
||||
|
||||
const [comment] = transactionRes.records.map(record => record.get('comment').properties)
|
||||
|
||||
const [comment] = await writeTxResultPromise
|
||||
return comment
|
||||
} finally {
|
||||
session.close()
|
||||
@ -39,15 +42,22 @@ export default {
|
||||
},
|
||||
UpdateComment: async (_parent, params, context, _resolveInfo) => {
|
||||
const session = context.driver.session()
|
||||
const writeTxResultPromise = session.writeTransaction(async transaction => {
|
||||
const updateCommentTransactionResponse = await transaction.run(
|
||||
`
|
||||
MATCH (comment:Comment {id: $params.id})
|
||||
SET comment += $params
|
||||
SET comment.updatedAt = toString(datetime())
|
||||
RETURN comment
|
||||
`,
|
||||
{ params },
|
||||
)
|
||||
return updateCommentTransactionResponse.records.map(
|
||||
record => record.get('comment').properties,
|
||||
)
|
||||
})
|
||||
try {
|
||||
const updateCommentCypher = `
|
||||
MATCH (comment:Comment {id: $params.id})
|
||||
SET comment += $params
|
||||
SET comment.updatedAt = toString(datetime())
|
||||
RETURN comment
|
||||
`
|
||||
const transactionRes = await session.run(updateCommentCypher, { params })
|
||||
const [comment] = transactionRes.records.map(record => record.get('comment').properties)
|
||||
const [comment] = await writeTxResultPromise
|
||||
return comment
|
||||
} finally {
|
||||
session.close()
|
||||
@ -55,18 +65,23 @@ export default {
|
||||
},
|
||||
DeleteComment: async (_parent, args, context, _resolveInfo) => {
|
||||
const session = context.driver.session()
|
||||
try {
|
||||
const transactionRes = await session.run(
|
||||
`
|
||||
MATCH (comment:Comment {id: $commentId})
|
||||
SET comment.deleted = TRUE
|
||||
SET comment.content = 'UNAVAILABLE'
|
||||
SET comment.contentExcerpt = 'UNAVAILABLE'
|
||||
RETURN comment
|
||||
`,
|
||||
const writeTxResultPromise = session.writeTransaction(async transaction => {
|
||||
const deleteCommentTransactionResponse = await transaction.run(
|
||||
`
|
||||
MATCH (comment:Comment {id: $commentId})
|
||||
SET comment.deleted = TRUE
|
||||
SET comment.content = 'UNAVAILABLE'
|
||||
SET comment.contentExcerpt = 'UNAVAILABLE'
|
||||
RETURN comment
|
||||
`,
|
||||
{ commentId: args.id },
|
||||
)
|
||||
const [comment] = transactionRes.records.map(record => record.get('comment').properties)
|
||||
return deleteCommentTransactionResponse.records.map(
|
||||
record => record.get('comment').properties,
|
||||
)
|
||||
})
|
||||
try {
|
||||
const [comment] = await writeTxResultPromise
|
||||
return comment
|
||||
} finally {
|
||||
session.close()
|
||||
|
||||
@ -10,7 +10,8 @@ const factory = Factory()
|
||||
|
||||
let variables, mutate, authenticatedUser, commentAuthor, newlyCreatedComment
|
||||
|
||||
beforeAll(() => {
|
||||
beforeAll(async () => {
|
||||
await factory.cleanDatabase()
|
||||
const { server } = createServer({
|
||||
context: () => {
|
||||
return {
|
||||
@ -19,8 +20,7 @@ beforeAll(() => {
|
||||
}
|
||||
},
|
||||
})
|
||||
const client = createTestClient(server)
|
||||
mutate = client.mutate
|
||||
mutate = createTestClient(server).mutate
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
@ -100,6 +100,7 @@ describe('CreateComment', () => {
|
||||
await expect(mutate({ mutation: createCommentMutation, variables })).resolves.toMatchObject(
|
||||
{
|
||||
data: { CreateComment: { content: "I'm authorised to comment" } },
|
||||
errors: undefined,
|
||||
},
|
||||
)
|
||||
})
|
||||
@ -108,6 +109,7 @@ describe('CreateComment', () => {
|
||||
await expect(mutate({ mutation: createCommentMutation, variables })).resolves.toMatchObject(
|
||||
{
|
||||
data: { CreateComment: { author: { name: 'Author' } } },
|
||||
errors: undefined,
|
||||
},
|
||||
)
|
||||
})
|
||||
@ -157,6 +159,7 @@ describe('UpdateComment', () => {
|
||||
it('updates the comment', async () => {
|
||||
const expected = {
|
||||
data: { UpdateComment: { id: 'c456', content: 'The comment is updated' } },
|
||||
errors: undefined,
|
||||
}
|
||||
await expect(mutate({ mutation: updateCommentMutation, variables })).resolves.toMatchObject(
|
||||
expected,
|
||||
@ -172,6 +175,7 @@ describe('UpdateComment', () => {
|
||||
createdAt: expect.any(String),
|
||||
},
|
||||
},
|
||||
errors: undefined,
|
||||
}
|
||||
await expect(mutate({ mutation: updateCommentMutation, variables })).resolves.toMatchObject(
|
||||
expected,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user