---
title: Chats
tags: subscription, chat
---
Tables:
jobPostChatThreads
chats
userChats
---
### Get user chat threads
**Request**
```
subscription getChatSubcription {
jobPostChatThreads(
where:{
startedBy:{_eq:"user uuid"}
}
) {
id
created_at
updated_at
startedBy
jobPostId
}
}
```
**Fields**
```
startedBy: User id of the recruiter that started this thread
jobPostId: Id of the job post
```
### Get all chat messages for a thread
**Request**
```
subscription getChatSubcription {
chats(
where:{
threadId:{_eq:"thread id"}
}
) {
id
message
type
created_at
updated_at
threadId
userId
user_chats(where: {userId: {_eq: ""}}}) {
read
}
}
}
```
**Fields**
```
message: Text
type: Type of message (Ignore for now)
threadId: Id of the job post
userId: The id of the user who sent this message
```
### Get all chat threads for a job post
**Request**
```
subscription getUserChatForJobPostSubcription {
jobPostChatThreads(
where:{
jobPostId:{_eq: "job post id"}
}
) {
id
created_at
updated_at
startedBy
jobPostId
}
}
```
### Get chat threads for a jobpost of a user
```
subscription getUserChatForJobPostSubcription {
jobPostChatThreads(
where:{
_and:[
{jobPostId:{_eq: "job post id"}}
{startedBy:{_eq: "user id"}}
]
}
) {
id
created_at
updated_at
startedBy
jobPostId
}
}
```
### Create a new thread
```
mutation ($objects: [jobPostChatThreads_insert_input!]!) {
insert_jobPostChatThreads(objects: $objects) {
returning {
id
created_at
updated_at
startedBy
jobPostId
}
}
}
```
**Fields**
```
startedBy: User id of the recruiter that started this thread
jobPostId: Id of the job post
```
### Insert a new message to a thread
```
mutation ($objects: [jobPostChats_insert_input!]!) {
insert_chats(objects: $objects) {
returning {
id
message
type
created_at
updated_at
threadId
userId
}
}
}
```
**Fields**
```
message: Text
type: Type of message, any string or null (Ignore for now)
threadId: Id of the thread
userId: The id of the user who sent this message
```
### Get unread message count for a thread
```
subscription getUnreadMessagesForAThread {
readCount: userChats_aggregate(where:{
_and:{
userId:{_eq:""}
chat:{
_and: {
threadId: {_eq: ""},
userId: {_neq: ""}
}
}
}
}) {
aggregate {
count
}
}
totalCount: chats_aggregate(where: {
_and: {
threadId: {_eq: ""},
userId: {_neq: ""}
}
}) {
aggregate {
count
}
}
}
```
You will get the unread count by subtracting totalCount with readCount
### Mark a chat as read for a user
```
mutation ($objects: [userChats_insert_input!]!) {
insert_userChats(objects: $objects) {
returning {
id
userId
chatId
read
}
}
}
```
**Fields**
```
userId
chatId
read: True/ False
```
**Note 1**: Ignore type for now. We might need it later that's why I added this.
**Note 2**: There aren't any checks in place right now. I'll add them afterwards. This wont hamper your work