Skip to content

Commit 20cb4fc

Browse files
author
Manav Gupta
committed
update: typeDef, Response Data, and getting allroom data using apollo client
1 parent 2544b47 commit 20cb4fc

File tree

6 files changed

+38
-11
lines changed

6 files changed

+38
-11
lines changed

backend/src/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,22 @@ const pubsub = new PubSub();
4747
name,
4848
description,
4949
},
50+
select: {
51+
id: true,
52+
name: true,
53+
description: true,
54+
messages: {
55+
select: {
56+
id: true,
57+
body: true,
58+
},
59+
},
60+
},
5061
});
5162
},
5263
createMessage: async (_: any, args: any) => {
5364
const { body, roomId, senderId } = args;
54-
const mesRes = await prisma.message.create({
65+
const messageResponse = await prisma.message.create({
5566
data: {
5667
body,
5768
senderId,
@@ -73,8 +84,10 @@ const pubsub = new PubSub();
7384
},
7485
},
7586
});
76-
pubsub.publish(`messageSent ${roomId}`, { messageSent: mesRes });
77-
return mesRes;
87+
pubsub.publish(`messageSent ${roomId}`, {
88+
messageSent: messageResponse,
89+
});
90+
return messageResponse;
7891
},
7992
},
8093
Subscription: {

backend/src/typeDefs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export const typeDefs = gql`
1010
type Message {
1111
id: String
1212
body: String
13-
senderId: String
14-
roomId: String
13+
sender: User
14+
room: Room
1515
}
1616
1717
type Room {

frontend/src/components/Chat.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ const client = new ApolloClient({
99
});
1010

1111
export const Chat = () => {
12-
const [chat, setChat] = useState<String>();
12+
const [chat, setChat] = useState<string>();
13+
14+
console.log(chat);
1315

1416
const onSelectRoom = (roomId: string) => {
1517
setChat(roomId);
1618
};
1719
return (
1820
<ApolloProvider client={client}>
1921
<div className="flex">
20-
<ChatBox roomId="a448cf91-4009-4442-81a1-2ad8d65fb451" />
22+
<ChatBox roomId={chat} />
2123
<Chatsidebar onSelectRoomChat={onSelectRoom} />
2224
</div>
2325
</ApolloProvider>

frontend/src/components/ChatBox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar";
55

66
import { Separator } from "./ui/separator";
77

8-
export const ChatBox = ({ roomId }: { roomId: string }) => {
8+
export const ChatBox = ({ roomId }: { roomId: string | undefined }) => {
99
useEffect(() => {
1010
console.log("roomId", roomId);
1111
}, [roomId]);

frontend/src/components/ChatSideBar.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ export const Chatsidebar = ({
5353
<p>Error :</p>
5454
) : (
5555
roomsData?.getAllRooms.map((room: Room) => {
56-
return <UserCard key={room.id} roomData={room} />;
56+
return (
57+
<UserCard
58+
onSelectRoom={onSelectRoomChat}
59+
key={room.id}
60+
roomData={room}
61+
/>
62+
);
5763
})
5864
);
5965

frontend/src/components/User.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { Room } from "../lib/types";
22
import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar";
33

4-
export const UserCard = ({ roomData }: { roomData: Room }) => {
4+
export const UserCard = ({
5+
roomData,
6+
onSelectRoom,
7+
}: {
8+
roomData: Room;
9+
onSelectRoom: (roomId: string) => void;
10+
}) => {
511
console.log(roomData);
612
return (
713
<li
814
onClick={() => {
9-
console.log(roomData.id);
15+
onSelectRoom(roomData.id);
1016
}}
1117
className="flex items-center gap-4 hover:bg-[#0000000f] py-1 px-2 cursor-pointer rounded-md"
1218
>

0 commit comments

Comments
 (0)