Module 01: Chatbot Service (API)1.5 GraphQL API & Code Examples
Module 01 • API Gateway
1.5.7 Đóng Phiên Chat (CloseChatSession)
MUTATION
Yêu cầu Bearer Token
CloseChatSession.gql
Variables
Response 200 OK
closeChatSession.ts
Đóng phiên chat hiện tại khi người dùng bấm "Bắt đầu cuộc trò chuyện mới", "Làm mới hội thoại" hoặc khi vấn đề hỗ trợ đã được giải quyết trọn vẹn. Sau khi đóng, các câu hỏi tiếp theo sẽ được coi là thuộc phiên chat mới (chatSessionId: null).
💻 Chi Tiết Truy Vấn & Mã Nguồn Mẫu
GraphQL Mutation & Payload:
GraphQL Mutation
mutation CloseChatSession($chatSessionId: String!) {
closeChatSession(chatSessionId: $chatSessionId)
}Variables (JSON)
{
"chatSessionId": "6a7d45b5aa2c04f1151bee2b"
}Phản Hồi Thành Công (JSON Response)
{
"data": {
"closeChatSession": true
}
}TypeScript / Axios:
import axios from 'axios';
/**
* Đóng phiên chat hiện tại
* @param apiUrl Endpoint GraphQL Gateway
* @param token JWT Access Token
* @param chatSessionId ID phiên chat cần đóng
*/
export async function closeChatSession(
apiUrl: string = process.env.VITE_API_URL!,
token: string,
chatSessionId: string
): Promise<boolean> {
const query = `
mutation CloseChatSession($chatSessionId: String!) {
closeChatSession(chatSessionId: $chatSessionId)
}
`;
const response = await axios.post(
apiUrl,
{
query,
variables: { chatSessionId },
operationName: 'CloseChatSession',
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'orgId': process.env.VITE_ORG_ID,
'x-org-id': process.env.VITE_ORG_ID,
'x-client-type': 'app',
'x-external-private-key': process.env.VITE_EXTERNAL_PRIVATE_KEY,
},
}
);
return !!response.data?.data?.closeChatSession;
}