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: {
|
Mutation: {
|
||||||
CreateComment: async (object, params, context, resolveInfo) => {
|
CreateComment: async (object, params, context, resolveInfo) => {
|
||||||
const { postId } = params
|
const { postId } = params
|
||||||
|
const { user, driver } = context
|
||||||
// Adding relationship from comment to post by passing in the postId,
|
// 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
|
// 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
|
// because we use relationships for this. So, we are deleting it from params
|
||||||
@ -12,9 +13,11 @@ export default {
|
|||||||
delete params.postId
|
delete params.postId
|
||||||
params.id = params.id || uuid()
|
params.id = params.id || uuid()
|
||||||
|
|
||||||
const session = context.driver.session()
|
const session = driver.session()
|
||||||
try {
|
|
||||||
const createCommentCypher = `
|
const writeTxResultPromise = session.writeTransaction(async transaction => {
|
||||||
|
const createCommentTransactionResponse = await transaction.run(
|
||||||
|
`
|
||||||
MATCH (post:Post {id: $postId})
|
MATCH (post:Post {id: $postId})
|
||||||
MATCH (author:User {id: $userId})
|
MATCH (author:User {id: $userId})
|
||||||
WITH post, author
|
WITH post, author
|
||||||
@ -23,15 +26,15 @@ export default {
|
|||||||
SET comment.updatedAt = toString(datetime())
|
SET comment.updatedAt = toString(datetime())
|
||||||
MERGE (post)<-[:COMMENTS]-(comment)<-[:WROTE]-(author)
|
MERGE (post)<-[:COMMENTS]-(comment)<-[:WROTE]-(author)
|
||||||
RETURN comment
|
RETURN comment
|
||||||
`
|
`,
|
||||||
const transactionRes = await session.run(createCommentCypher, {
|
{ userId: user.id, postId, params },
|
||||||
userId: context.user.id,
|
)
|
||||||
postId,
|
return createCommentTransactionResponse.records.map(
|
||||||
params,
|
record => record.get('comment').properties,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
try {
|
||||||
const [comment] = transactionRes.records.map(record => record.get('comment').properties)
|
const [comment] = await writeTxResultPromise
|
||||||
|
|
||||||
return comment
|
return comment
|
||||||
} finally {
|
} finally {
|
||||||
session.close()
|
session.close()
|
||||||
@ -39,15 +42,22 @@ export default {
|
|||||||
},
|
},
|
||||||
UpdateComment: async (_parent, params, context, _resolveInfo) => {
|
UpdateComment: async (_parent, params, context, _resolveInfo) => {
|
||||||
const session = context.driver.session()
|
const session = context.driver.session()
|
||||||
try {
|
const writeTxResultPromise = session.writeTransaction(async transaction => {
|
||||||
const updateCommentCypher = `
|
const updateCommentTransactionResponse = await transaction.run(
|
||||||
|
`
|
||||||
MATCH (comment:Comment {id: $params.id})
|
MATCH (comment:Comment {id: $params.id})
|
||||||
SET comment += $params
|
SET comment += $params
|
||||||
SET comment.updatedAt = toString(datetime())
|
SET comment.updatedAt = toString(datetime())
|
||||||
RETURN comment
|
RETURN comment
|
||||||
`
|
`,
|
||||||
const transactionRes = await session.run(updateCommentCypher, { params })
|
{ params },
|
||||||
const [comment] = transactionRes.records.map(record => record.get('comment').properties)
|
)
|
||||||
|
return updateCommentTransactionResponse.records.map(
|
||||||
|
record => record.get('comment').properties,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
try {
|
||||||
|
const [comment] = await writeTxResultPromise
|
||||||
return comment
|
return comment
|
||||||
} finally {
|
} finally {
|
||||||
session.close()
|
session.close()
|
||||||
@ -55,8 +65,8 @@ export default {
|
|||||||
},
|
},
|
||||||
DeleteComment: async (_parent, args, context, _resolveInfo) => {
|
DeleteComment: async (_parent, args, context, _resolveInfo) => {
|
||||||
const session = context.driver.session()
|
const session = context.driver.session()
|
||||||
try {
|
const writeTxResultPromise = session.writeTransaction(async transaction => {
|
||||||
const transactionRes = await session.run(
|
const deleteCommentTransactionResponse = await transaction.run(
|
||||||
`
|
`
|
||||||
MATCH (comment:Comment {id: $commentId})
|
MATCH (comment:Comment {id: $commentId})
|
||||||
SET comment.deleted = TRUE
|
SET comment.deleted = TRUE
|
||||||
@ -66,7 +76,12 @@ export default {
|
|||||||
`,
|
`,
|
||||||
{ commentId: args.id },
|
{ 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
|
return comment
|
||||||
} finally {
|
} finally {
|
||||||
session.close()
|
session.close()
|
||||||
|
|||||||
@ -10,7 +10,8 @@ const factory = Factory()
|
|||||||
|
|
||||||
let variables, mutate, authenticatedUser, commentAuthor, newlyCreatedComment
|
let variables, mutate, authenticatedUser, commentAuthor, newlyCreatedComment
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(async () => {
|
||||||
|
await factory.cleanDatabase()
|
||||||
const { server } = createServer({
|
const { server } = createServer({
|
||||||
context: () => {
|
context: () => {
|
||||||
return {
|
return {
|
||||||
@ -19,8 +20,7 @@ beforeAll(() => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const client = createTestClient(server)
|
mutate = createTestClient(server).mutate
|
||||||
mutate = client.mutate
|
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
@ -100,6 +100,7 @@ describe('CreateComment', () => {
|
|||||||
await expect(mutate({ mutation: createCommentMutation, variables })).resolves.toMatchObject(
|
await expect(mutate({ mutation: createCommentMutation, variables })).resolves.toMatchObject(
|
||||||
{
|
{
|
||||||
data: { CreateComment: { content: "I'm authorised to comment" } },
|
data: { CreateComment: { content: "I'm authorised to comment" } },
|
||||||
|
errors: undefined,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -108,6 +109,7 @@ describe('CreateComment', () => {
|
|||||||
await expect(mutate({ mutation: createCommentMutation, variables })).resolves.toMatchObject(
|
await expect(mutate({ mutation: createCommentMutation, variables })).resolves.toMatchObject(
|
||||||
{
|
{
|
||||||
data: { CreateComment: { author: { name: 'Author' } } },
|
data: { CreateComment: { author: { name: 'Author' } } },
|
||||||
|
errors: undefined,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -157,6 +159,7 @@ describe('UpdateComment', () => {
|
|||||||
it('updates the comment', async () => {
|
it('updates the comment', async () => {
|
||||||
const expected = {
|
const expected = {
|
||||||
data: { UpdateComment: { id: 'c456', content: 'The comment is updated' } },
|
data: { UpdateComment: { id: 'c456', content: 'The comment is updated' } },
|
||||||
|
errors: undefined,
|
||||||
}
|
}
|
||||||
await expect(mutate({ mutation: updateCommentMutation, variables })).resolves.toMatchObject(
|
await expect(mutate({ mutation: updateCommentMutation, variables })).resolves.toMatchObject(
|
||||||
expected,
|
expected,
|
||||||
@ -172,6 +175,7 @@ describe('UpdateComment', () => {
|
|||||||
createdAt: expect.any(String),
|
createdAt: expect.any(String),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
errors: undefined,
|
||||||
}
|
}
|
||||||
await expect(mutate({ mutation: updateCommentMutation, variables })).resolves.toMatchObject(
|
await expect(mutate({ mutation: updateCommentMutation, variables })).resolves.toMatchObject(
|
||||||
expected,
|
expected,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user