Chat Operations
This section covers GraphQL operations related to the chat functionality.
Queries​
All Channels​
Retrieves all public live stream channels.
query AllChannels {
allChannels {
id
type
name
avatar
}
}
Response:
{
"data": {
"allChannels": [
{
"id": "channel-id-1",
"type": "livestream",
"name": "General Chat",
"avatar": "https://example.com/channel-avatar.jpg"
},
{
"id": "channel-id-2",
"type": "livestream",
"name": "News",
"avatar": null
}
]
}
}
All User Chats​
Retrieves all chats for the currently authenticated user with pagination.
query AllUserChats(
$limit: Int
$offset: Int
$order: SortOrder
$orderBy: ChatStreamChannelOrderBy
) {
allUserChats(
limit: $limit
offset: $offset
order: $order
orderBy: $orderBy
) {
data {
id
}
limit
offset
}
}
Input Parameters:
{
"limit": 10,
"offset": 0,
"order": "DESC",
"orderBy": "LAST_MESSAGE_AT"
}
Response:
{
"data": {
"allUserChats": {
"data": [
{
"id": "chat-id-1"
},
{
"id": "chat-id-2"
}
],
"limit": 10,
"offset": 0
}
}
}
Mutations​
Create Chat with Other Users​
Creates a new chat channel with specified users.
mutation CreateChatWithOtherUsers($input: CreateChatWithUsersInput!) {
createChatWithOtherUsers(input: $input)
}
Input Parameters:
{
"input": {
"userIds": ["user-id-1", "user-id-2"]
}
}
Response:
{
"data": {
"createChatWithOtherUsers": true
}
}
Chat Interaction​
After retrieving chat channels and creating chats, you'll need to use the Stream Chat client library to interact with the chats. Use the streamChatToken
query to get a token for the Stream Chat API.
Stream Chat Integration​
- Get Stream Chat Token
query StreamChatToken {
streamChatToken
}
- Initialize Stream Chat Client
Use the token to initialize the Stream Chat client in your application:
// Example using JavaScript Stream Chat client
const chatClient = StreamChat.getInstance(apiKey);
await chatClient.connectUser({ id: userId }, streamChatToken);
- Access Chat Channels
After connecting to Stream Chat, you can access the channels retrieved from the GraphQL API:
// Example using JavaScript Stream Chat client
const channel = chatClient.channel('livestream', channelId);
await channel.watch();
Channel Types​
livestream
: Public channel visible to all usersmessaging
: Private channel between specific users
Input Types​
ChatStreamChannelsArgs​
Field | Type | Description | Default |
---|---|---|---|
offset | Int | Number of items to skip | 0 |
limit | Int | Number of items to take | 10 |
order | SortOrder | Sort order (ASC or DESC) | DESC |
orderBy | ChatStreamChannelOrderBy | Field to sort by | LAST_MESSAGE_AT |
CreateChatWithUsersInput​
Field | Type | Description | Required |
---|---|---|---|
userIds | [String!] | List of valid MongoDB user IDs to create chat with | Yes |
ChatStreamChannelOrderBy enum values​
CREATED_AT
: Sort by channel creation timeLAST_MESSAGE_AT
: Sort by time of the last message (default)LAST_UPDATED
: Sort by channel update time