diff --git a/backend/src/graphql/messages.ts b/backend/src/graphql/messages.ts index 59694914a..c51950cc8 100644 --- a/backend/src/graphql/messages.ts +++ b/backend/src/graphql/messages.ts @@ -21,11 +21,13 @@ export const messageQuery = () => { return gql` query($roomId: ID!) { Message(roomId: $roomId) { + _id id content - author { - id - } + senderId + username + avatar + date } } ` diff --git a/backend/src/graphql/rooms.ts b/backend/src/graphql/rooms.ts index 38d10a1d8..1c2120fb0 100644 --- a/backend/src/graphql/rooms.ts +++ b/backend/src/graphql/rooms.ts @@ -9,6 +9,7 @@ export const createRoomMutation = () => { userId: $userId ) { id + roomId } } ` @@ -19,8 +20,15 @@ export const roomQuery = () => { query { Room { id + roomId + roomName users { + _id id + name + avatar { + url + } } } } diff --git a/backend/src/schema/resolvers/messages.spec.ts b/backend/src/schema/resolvers/messages.spec.ts index e9cf26a22..3ee905be9 100644 --- a/backend/src/schema/resolvers/messages.spec.ts +++ b/backend/src/schema/resolvers/messages.spec.ts @@ -184,20 +184,23 @@ describe('Message', () => { describe('room exists with authenticated user chatting', () => { it('returns the messages', async () => { - await expect(query({ + const result = await query({ query: messageQuery(), variables: { roomId, }, - })).resolves.toMatchObject({ + }) + expect(result).toMatchObject({ errors: undefined, data: { Message: [{ id: expect.any(String), + _id: result.data.Message[0].id, content: 'Some nice message to other chatting user', - author: { - id: 'chatting-user', - }, + senderId: 'chatting-user', + username: 'Chatting User', + avatar: expect.any(String), + date: expect.any(String), }], }, }) @@ -209,9 +212,17 @@ describe('Message', () => { mutation: createMessageMutation(), variables: { roomId, - content: 'Another nice message to other chatting user', + content: 'A nice response message to chatting user', } }) + authenticatedUser = await chattingUser.toJson() + await mutate({ + mutation: createMessageMutation(), + variables: { + roomId, + content: 'And another nice message to other chatting user', + } + }) }) it('returns the messages', async () => { @@ -227,22 +238,32 @@ describe('Message', () => { { id: expect.any(String), content: 'Some nice message to other chatting user', - author: { - id: 'chatting-user', - }, + senderId: 'chatting-user', + username: 'Chatting User', + avatar: expect.any(String), + date: expect.any(String), }, { id: expect.any(String), - content: 'Another nice message to other chatting user', - author: { - id: 'other-chatting-user', - }, - } + content: 'A nice response message to chatting user', + senderId: 'other-chatting-user', + username: 'Other Chatting User', + avatar: expect.any(String), + date: expect.any(String), + }, + { + id: expect.any(String), + content: 'And another nice message to other chatting user', + senderId: 'chatting-user', + username: 'Chatting User', + avatar: expect.any(String), + date: expect.any(String), + }, ], }, }) }) - }) + }) }) describe('room exists, authenticated user not in room', () => { diff --git a/backend/src/schema/resolvers/messages.ts b/backend/src/schema/resolvers/messages.ts index 2cf72e9fe..0be0298d1 100644 --- a/backend/src/schema/resolvers/messages.ts +++ b/backend/src/schema/resolvers/messages.ts @@ -13,7 +13,13 @@ export default { id: context.user.id, }, } - return neo4jgraphql(object, params, context, resolveInfo) + const resolved = await neo4jgraphql(object, params, context, resolveInfo) + if (resolved) { + resolved.forEach((message) => { + message._id = message.id + }) + } + return resolved }, }, Mutation: { @@ -24,11 +30,11 @@ export default { const writeTxResultPromise = session.writeTransaction(async (transaction) => { const createMessageCypher = ` MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId }) - MERGE (currentUser)-[:CREATED]->(message:Message)-[:INSIDE]->(room) - ON CREATE SET - message.createdAt = toString(datetime()), - message.id = apoc.create.uuid(), - message.content = $content + CREATE (currentUser)-[:CREATED]->(message:Message { + createdAt: toString(datetime()), + id: apoc.create.uuid(), + content: $content + })-[:INSIDE]->(room) RETURN message { .* } ` const createMessageTxResponse = await transaction.run( diff --git a/backend/src/schema/resolvers/rooms.spec.ts b/backend/src/schema/resolvers/rooms.spec.ts index 8c4d887cb..945facd05 100644 --- a/backend/src/schema/resolvers/rooms.spec.ts +++ b/backend/src/schema/resolvers/rooms.spec.ts @@ -102,11 +102,12 @@ describe('Room', () => { }, }) roomId = result.data.CreateRoom.id - await expect(result).toMatchObject({ + expect(result).toMatchObject({ errors: undefined, data: { CreateRoom: { id: expect.any(String), + roomId: result.data.CreateRoom.id, }, }, }) @@ -153,18 +154,31 @@ describe('Room', () => { }) it('returns the room', async () => { - await expect(query({ query: roomQuery() })).resolves.toMatchObject({ + const result = await query({ query: roomQuery() }) + expect(result).toMatchObject({ errors: undefined, data: { Room: [ { id: expect.any(String), + roomId: result.data.Room[0].id, + roomName: 'Other Chatting User', users: expect.arrayContaining([ { + _id: 'chatting-user', id: 'chatting-user', + name: 'Chatting User', + avatar: { + url: expect.any(String), + }, }, { + _id: 'other-chatting-user', id: 'other-chatting-user', + name: 'Other Chatting User', + avatar: { + url: expect.any(String), + }, }, ]), }, @@ -180,18 +194,31 @@ describe('Room', () => { }) it('returns the room', async () => { - await expect(query({ query: roomQuery() })).resolves.toMatchObject({ + const result = await query({ query: roomQuery() }) + expect(result).toMatchObject({ errors: undefined, data: { Room: [ { id: expect.any(String), + roomId: result.data.Room[0].id, + roomName: 'Chatting User', users: expect.arrayContaining([ { + _id: 'chatting-user', id: 'chatting-user', + name: 'Chatting User', + avatar: { + url: expect.any(String), + }, }, { + _id: 'other-chatting-user', id: 'other-chatting-user', + name: 'Other Chatting User', + avatar: { + url: expect.any(String), + }, }, ]), }, diff --git a/backend/src/schema/resolvers/rooms.ts b/backend/src/schema/resolvers/rooms.ts index f3ea05cc9..bf0e6b8a6 100644 --- a/backend/src/schema/resolvers/rooms.ts +++ b/backend/src/schema/resolvers/rooms.ts @@ -8,7 +8,18 @@ export default { params.filter.users_some = { id: context.user.id, } - return neo4jgraphql(object, params, context, resolveInfo) + const resolved = await neo4jgraphql(object, params, context, resolveInfo) + if (resolved) { + resolved.forEach((room) => { + if (room.users) { + room.roomName = room.users.filter((user) => user.id !== context.user.id)[0].name + room.users.forEach((user) => { + user._id = user.id + }) + } + }) + } + return resolved }, }, Mutation: { @@ -37,6 +48,9 @@ export default { }) try { const room = await writeTxResultPromise + if (room) { + room.roomId = room.id + } return room } catch (error) { throw new Error(error) diff --git a/backend/src/schema/types/type/Message.gql b/backend/src/schema/types/type/Message.gql index 5b14104db..4a3346079 100644 --- a/backend/src/schema/types/type/Message.gql +++ b/backend/src/schema/types/type/Message.gql @@ -11,6 +11,11 @@ type Message { author: User! @relation(name: "CREATED", direction: "IN") room: Room! @relation(name: "INSIDE", direction: "OUT") + + senderId: String! @cypher(statement: "MATCH (this)<-[:CREATED]-(user:User) RETURN user.id") + username: String! @cypher(statement: "MATCH (this)<-[:CREATED]-(user:User) RETURN user.name") + avatar: String @cypher(statement: "MATCH (this)<-[:CREATED]-(:User)-[:AVATAR_IMAGE]->(image:Image) RETURN image.url") + date: String! @cypher(statement: "RETURN this.createdAt") } type Mutation { diff --git a/backend/src/schema/types/type/Room.gql b/backend/src/schema/types/type/Room.gql index 8792aa56a..c90ebda3a 100644 --- a/backend/src/schema/types/type/Room.gql +++ b/backend/src/schema/types/type/Room.gql @@ -10,7 +10,10 @@ type Room { createdAt: String updatedAt: String - users: [User]! @relation(name: "CHATS_IN", direction: "IN") + users: [User]! @relation(name: "CHATS_IN", direction: "IN") + + roomId: String! @cypher(statement: "RETURN this.id") + roomName: String! ## @cypher(statement: "MATCH (this)<-[:CHATS_IN]-(user:User) WHERE NOT user.id = $cypherParams.currentUserId RETURN user[0].name") } type Mutation {