- Overview
- UML Model Diagram
- Endpoint
- Queries
- Mutations
- Subscriptions
- Types
- Mutation Types
- CreateGroup
- CreateGroupMessage
- CreateGroupMember
- ChangeAdmin
- UpdateGroup
- DeleteGroup
- UpdateGroupMessage
- DeleteGroupMessage
- UnsendGroupMessage
- RemoveGroupMember
- LeaveGroup
- RemoveGroupPermanently
- SetArchiveGroup
- SetNotificationAsRead
- CreateUser
- UpdateUser
- ChangePassword
- DeleteUser
- CreateUser
- AddPhoneNumber
- RemovePhoneNumber
- CreateChat
- CreateSelfChat
- CreateChatMessage
- UpdateChatMessage
- DeleteChatMessage
- UnsendChatMessage
- DeleteChat
- SetChatArchived
- SetChatMessageAsRead
- Subscription Types
- Other Features
- Conclusion
This is the API documentation for the BuddyChat API. The API is built with Django and GraphQL using graphene. The API introduces a chat system with the following features:
- User registration and authentication
- Chat system
- Group chat system
- Notifications
- Real-time chat messages
- Copy of messages for each user which leads to the ability to delete messages for each user
The API endpoint is /graphql
.
users
: The users in the systemchats
: The chats for the current usergroups
: The group copies for the current usernotifications
: The notifications for the current userchat
: Resolve a chat for the current usergroup
: Resolve a group copy for the current useruser
: Resolve a user
setNotificationRead
: Set a notification as readlogin
: Login to the API. Returns a tokenrefreshToken
: Refresh the tokenverifyToken
: Verify the tokenrevokeToken
: Revoke the tokencreateUser
: Create a user with a phone numberaddPhoneNumber
: Add a phone number to the current userremovePhoneNumber
: Remove a phone number from the current userupdateUser
: Update the current user's datachangePassword
: Change the current user's passworddeleteUser
: Delete the current usercreateGroup
: A mutation to create a group. The creator is automatically added as an admin, and a group member copy is created for the creatorcreateGroupMessage
: A mutation to create a group message. A group message is created for each group member, and a notification is created for each group membercreateGroupMember
: A mutation to add a member to a groupchangeAdmin
: A mutation to change the admin status of a group memberupdateGroup
: A mutation to update a groupdeleteGroup
: A mutation to delete a group. Messages are deleted for the group copy of the current userupdateGroupMessage
: A mutation to update a group messagedeleteGroupMessage
: A mutation to delete a group message. Deletes the message for the group copy of the current userunsendGroupMessage
: A mutation to unsend a group message. Deletes the message for all group membersremoveGroupMember
: A mutation to remove a member from a groupleaveGroup
: A mutation to leave a groupremoveGroupPermanently
: A mutation to remove a group permanentlysetArchiveGroup
: A mutation to archive a groupcreateChat
: A mutation to create two chat copies for two userscreateSelfChat
: A mutation to create a chat copy for the current usercreateChatMessage
: A mutation to create a chat message. A chat message is created for each user, and a notification is created for the other userupdateChatMessage
: A mutation to update a chat messagedeleteChatMessage
: A mutation to delete a chat message. Deletes the message for the chat copy of the current userunsendChatMessage
: A mutation to unsend a chat message. Deletes the message for both chat copiesdeleteChat
: A mutation to delete a chat. Messages are deleted for the chat copy of the current usersetArchiveChat
: A mutation to archive or unarchive a chatsetChatMessageAsRead
: A mutation to set a chat message as read
-
The API uses
Django Channels
withsignals
to send messages to the client. The messages which are sent are not much likeGraphQL
subscriptions. They are more likeWebSocket
messages. Because ofGraphene
limitations, I made a workaround to send messages likeWebSocket
messages. The subprotocol used isgraphql-transport-ws
. -
The subscription is done by sending a
subscription
query to the API with the following structure:subscription { subscribe { success } }
-
This query is done to subscribe to the messages. The
success
field returnsTrue
and it is not used. The subscription is done by sending anext
message to the client with the message payload.
- All messages are sent as
next
messages and contain the fieldid
which represents the subscription id. - The message contains
payload
which contains the actual message. - The
payload
message is an object with the following properties:type
: The type of the message. It is alwaysbroadcast
.operation
: The operation which is done.payload
: The actual payload of the message.
Operation | Description |
---|---|
CHAT_MESSAGE_CREATED | A new chat message has been created |
CHAT_MESSAGE_UPDATED | An existing chat message has been updated |
CHAT_MESSAGE_DELETED | A chat message has been deleted |
CHAT_MESSAGE_UNSENT | A chat message has been unsent |
CHAT_MESSAGE_READ | A chat message has been read |
CHAT_DELETED | A chat has been deleted |
GROUP_MESSAGE_CREATED | A new group message has been created |
GROUP_MESSAGE_UPDATED | An existing group message has been updated |
GROUP_MESSAGE_DELETED | A group message has been deleted |
GROUP_MESSAGE_UNSENT | A group message has been unsent |
GROUP_MESSAGE_READ | A group message has been read |
GROUP_DELETED | A group has been deleted |
GROUP_UPDATED | A group has been updated |
GROUP_PERMANENTLY_REMOVED | A group has been permanently removed |
NOTIFICATION_CREATED | A new notification has been created |
MEMBER_ADDED | A new member has been added to a group |
MEMBER_REMOVED | A member has been removed from a group |
MEMBER_LEFT | A member has left a group |
{
"type": "next",
"id": "b7455cd0-2363-481a-913e-74be16be060e",
"payload": {
"type": "broadcast",
"operation": "CHAT_MESSAGE_CREATED",
"payload": {
"chatMessage": {
"id": "GQo6U2hhdHRlY2g6MjI=",
"message": {
"id": "GQo6TWVzc2FnZVR5cGU6MjA=",
"content": "Hello",
"sender": {
"id": "GQo6VXNlcjp1c2VyMQ=="
}
},
"chat": {
"id": "GQo6Q2hhdDpjaGF0MQ=="
}
}
}
}
}
type CustomUser {
id: ID!
username: String!
firstName: String!
lastName: String!
email: String!
phoneNumbers: [PhoneNumber]
chats: [Chat]
notifications: [Notification]
}
Description: The user type. It contains the user's information.
type PhoneNumber {
id: ID!
number: String!
countryCode: String!
}
input PhoneNumberInput {
number: String!
countryCode: String!
}
type Message {
id: ID!
content: String!
readAt: DateTime
}
Description: The root message type which is the dependent type for the chat message and group message types.
type Chat {
id: ID!
user: CustomUser!
otherUser: CustomUser!
archived: Boolean!
chat_messages: [ChatMessage]
}
Description: The chat type. Each user has a chat copy for each other user they have chatted with.
type ChatMessage {
id: ID!
message: Message!
chat: Chat!
}
Description: The chat message type. It contains the chat message information.
type UserGroup {
id: ID!
title: String!
description: String!
members: [GroupMember]
}
Description: The root user group type. It contains the main information. GroupMember depends on this type.
type GroupMessage {
id: ID!
message: Message!
group_copy: UserGroupMemberCopy!
}
Description: The group message type. It contains the group message information.
type GroupMember {
id: ID!
user: CustomUser!
group: UserGroup!
isAdmin: Boolean!
joinedAt: DateTime!
}
Description: The group member type. It contains the group member information.
type Attachment {
id: ID!
file: String!
message: Message!
}
type Notification {
id: ID!
message: Message!
readAt: DateTime
}
type UserGroupMemberCopy {
id: ID!
member: GroupMember!
isArchived: Boolean!
group_messages: [GroupMessage]
}
Description: The user group member copy type. The user copy of the group, so that each user can have a copy of the group messages.
type CreateGroup {
userGroup: UserGroup
}
Description: A mutation to create a group. The creator is automatically added as an admin, and a group member copy is created for the creator.
type CreateGroupMessage {
message: GroupMessage
}
Description: A mutation to create a group message. A group message is created for each group member, and a notification is created for each group member.
type CreateGroupMember {
groupMember: GroupMember
}
Description: A mutation to add a member to a group.
type ChangeAdmin {
groupMember: GroupMember
}
Description: A mutation to change the admin status of a group member.
type UpdateGroup {
groupCopy: UserGroupMemberCopy
}
Description: A mutation to update a group.
type DeleteGroup {
success: Boolean
}
Description: A mutation to delete a group. Messages are deleted for the group copy of the current user.
type UpdateGroupMessage {
groupMessage: GroupMessage
}
Description: A mutation to update a group message.
type DeleteGroupMessage {
success: Boolean
}
Description: A mutation to delete a group message. Deletes the message for the group copy of the current user.
type UnsendGroupMessage {
success: Boolean
}
Description: A mutation to unsend a group message. Deletes the message for all group members.
type RemoveGroupMember {
success: Boolean
}
Description: A mutation to remove a member from a group.
type LeaveGroup {
success: Boolean
}
Description: A mutation to leave a group.
type RemoveGroupPermanently {
success: Boolean
}
Description: A mutation to remove a group permanently.
type SetArchiveGroup {
groupCopy: UserGroupMemberCopy
}
Description: A mutation to archive a group.
type SetNotificationAsRead {
notification: Notification
}
Description: A mutation to set a notification as read.
type UpdateUser {
user: CustomUser
}
Description: Mutation to update the current user's data.
type ChangePassword {
user: CustomUser
}
Description: Mutation to change the current user's password.
type DeleteUser {
userId: Int
}
Description: Mutation to delete the current user.
type CreateUser {
user: CustomUser
phoneNumber: PhoneNumber
}
Description: Mutation to create a user with a phone number.
type AddPhoneNumber {
phoneNumber: PhoneNumber
}
Description: Mutation to add a phone number to the current user.
type RemovePhoneNumber {
success: Boolean
}
Description: Mutation to remove a phone number from the current user.
type CreateChat {
chat: Chat
otherChat: Chat
}
Description: Mutation to create two chat copies for two users.
type CreateSelfChat {
chat: Chat
}
Description: Mutation to create a chat for the user.
type CreateChatMessage {
chatMessage: ChatMessage
}
Description: Mutation to create a chat message. A chat message is created for each user, and a notification is created for the other user.
type UpdateChatMessage {
chatMessage: ChatMessage
}
Description: Mutation to update a chat message.
type DeleteChatMessage {
success: Boolean
}
Description: Mutation to delete a chat message. Deletes the message for the chat copy of the current user.
type UnsendChatMessage {
success: Boolean
}
Description: Mutation to unsend a chat message. Deletes the message for both chat copies.
type DeleteChat {
success: Boolean
}
Description: Mutation to delete a chat. Messages are deleted for the chat copy of the current user.
type SetChatArchived {
chat: Chat
}
Description: Mutation to archive or unarchive a chat.
type SetChatMessageAsRead {
chatMessage: ChatMessage
}
Description: Mutation to set a chat message as read.
type Subscribe {
success: Boolean
}
Description: The success message is returned when the client subscribes to the messages.
To ensure fair usage and prevent abuse, the BuddyChat API implements throttling. This limits the number of requests a user can make to the API within a certain time frame. If the limit is exceeded, the API will return a 403
error response.
The BuddyChat API supports pagination for queries that return lists of items. This helps to manage large datasets by breaking them into smaller, more manageable chunks. Pagination is implemented using the first
and after
arguments for forward pagination, and last
and before
arguments for backward pagination. The API returns a PageInfo
object that includes information about the current page and whether there are more pages available.
To provide more flexibility in querying data, the BuddyChat API supports filtering on various fields. Filters can be applied to queries to narrow down the results based on specific criteria. The available filters are documented for each query type.
The BuddyChat API provides a comprehensive set of features for building a robust chat application. With support for user authentication, real-time messaging, group chats, notifications, and more, it offers a solid foundation for any chat-based application. The API is designed to be flexible and scalable, with features like throttling, pagination, and filtering to handle various use cases and ensure optimal performance. Whether you're building a simple chat app or a complex messaging platform, the BuddyChat API has you covered.