simple create room mutation

This commit is contained in:
Moriz Wahl 2023-06-14 12:09:52 +02:00 committed by Ulf Gebhardt
parent 5a3b2bfb07
commit aecc50fceb
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
3 changed files with 48 additions and 0 deletions

View File

@ -459,6 +459,7 @@ export default shield(
switchUserRole: isAdmin,
markTeaserAsViewed: allow,
saveCategorySettings: isAuthenticated,
CreateRoom: isAuthenticated,
},
User: {
email: or(isMyOwn, isAdmin),

View File

@ -0,0 +1,34 @@
export default {
Mutation: {
CreateRoom: async (_parent, params, context, _resolveInfo) => {
const { userId } = params
const { user: { id: currentUserId } } = context
const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
const createRoomCypher = `
MATCH (currentUser:User { id: $currentUserId })
MATCH (user:User { id: $userId })
MERGE (currentUser)-[:CHATS_IN]->(room:Room)<-[:CHATS_IN]-(user)
SET room.createdAt = toString(datetime())
RETURN room
`
const createRommTxResponse = await await transaction.run(
createRoomCypher,
{ userId, currentUserId }
)
const [room] = await createRommTxResponse.records.map((record) =>
record.get('room'),
)
return room
})
try {
const room = await writeTxResultPromise
return room
} catch (error) {
throw new Error(error)
} finally {
session.close()
}
}
}
}

View File

@ -0,0 +1,13 @@
type Room {
id: ID!
createdAt: String
updatedAt: String
users: [User!]! @relation(name: "CHATS_IN", direction: "IN")
}
type Mutation {
CreateRoom(
userId: ID!
): Room
}