Welcome to this sample project that demonstrates how to use GraphQL subscriptions, particularly in the context of a chat bot, (that may use OpenAI's chat completions under the hood). This project is a practical guide to understand and implement GraphQL subscriptions using HotChocolate.
To get started with this project, follow these simple steps:
- Run the Project: Use the command
dotnet run
to start the project. - Access GraphQL Editor: Navigate to
http://localhost:5000/graphql
on your browser to open the GraphQL editor.
Here's a step-by-step guide to test the GraphQL subscriptions:
Initiate a new chat using the following GraphQL mutation:
mutation CreateChat {
createChat {
chat {
id
status
}
errors {
... on UnauthorizedError {
message
}
}
}
}
In a new tab within the editor, subscribe to chat messages updates using the following subscription:
subscription onChatMessagesUpdated($chatId: ID!) {
onChatMessagesUpdated(chatId: $chatId) {
... on ChatMessageCreated {
message {
...ChatMessage
}
}
... on ChatMessageUpdated {
message {
...ChatMessage
}
}
}
}
fragment ChatMessage on ChatMessage {
id
content
role
sentAt
}
Send a new message to the chat using the mutation below:
mutation SendMessage($chatId: ID!, $content: String!) {
sendMessage(
input: {
chatId: $chatId
content: $content
}
) {
message {
id
content
role
sentAt
}
errors {
... on ChatClosedError {
message
}
... on ChatNotFoundError {
message
}
... on ChatNotReadyForMessageError {
message
}
... on UnauthorizedError {
message
}
}
}
}
Watch the subscription tab for incoming messages. Notice how the subscription also notifies you of partial message updates from the assistant.
Attempt to execute the 'send message' mutation again and observe that it gets blocked while the assistant is replying.
Close the chat using the following mutation:
mutation closeChat($chatId: ID!) {
closeChat(input: {
chatId: $chatId
}){
chat {
id
status
}
errors {
... on UnauthorizedError {
message
}
... on ChatNotFoundError {
message
}
}
}
}
Watch the subscription tab to see the completion of the subscription.
Try executing the 'send message' mutation again and observe that it is blocked due to the chat being closed.
- HotChocolate: https://chillicream.com/docs/hotchocolate
- Slack: https://slack.chillicream.com/
- ChilliCream: https://chillicream.com/