Skip to main content

Overview

The RealtimeChat GraphQL API provides queries to fetch chat rooms and messages. All queries are available through the root Query type.

GetChatRoom

Retrieve a single chat room by its ID, including all associated messages.

Parameters

roomId
Int!
required
The unique identifier of the chat room to retrieve

Return Type

ChatRoomGraph - A chat room object with all its messages

GraphQL Schema

type Query {
  getChatRoom(roomId: Int!): ChatRoomGraph!
}

type ChatRoomGraph {
  id: Int!
  name: String!
  messages: [MessageGraph!]!
}

Example Query

query GetChatRoom {
  getChatRoom(roomId: 1) {
    id
    name
    messages {
      id
      senderId
      sentAt
      content {
        ... on TextMessageContentGraph {
          text
        }
      }
    }
  }
}

Example Response

{
  "data": {
    "getChatRoom": {
      "id": 1,
      "name": "General",
      "messages": [
        {
          "id": 101,
          "senderId": "user123",
          "sentAt": "2026-03-05T10:30:00Z",
          "content": {
            "text": "Hello everyone!"
          }
        },
        {
          "id": 102,
          "senderId": "user456",
          "sentAt": "2026-03-05T10:32:00Z",
          "content": {
            "text": "Hi there!"
          }
        }
      ]
    }
  }
}
This query will throw an error if the chat room with the specified ID does not exist.

GetChatRooms

Retrieve all available chat rooms. This query returns an IQueryable that supports filtering and pagination.

Parameters

This query accepts no required parameters but supports HotChocolate’s built-in filtering and pagination:
first
Int
Limit the number of results (pagination)
after
String
Cursor for forward pagination
where
ChatRoomGraphFilterInput
Filter chat rooms by criteria

Return Type

[ChatRoomGraph!]! - An array of chat room objects

GraphQL Schema

type Query {
  getChatRooms: [ChatRoomGraph!]!
}

type ChatRoomGraph {
  id: Int!
  name: String!
  messages: [MessageGraph!]!
}

Example Query

query GetAllChatRooms {
  getChatRooms {
    id
    name
    messages {
      id
      senderId
      sentAt
    }
  }
}

Example with Pagination

query GetChatRoomsPaginated {
  getChatRooms(first: 10) {
    id
    name
  }
}

Example Response

{
  "data": {
    "getChatRooms": [
      {
        "id": 1,
        "name": "General",
        "messages": []
      },
      {
        "id": 2,
        "name": "Random",
        "messages": []
      },
      {
        "id": 3,
        "name": "Development",
        "messages": []
      }
    ]
  }
}

GetMessages

Retrieve all messages from a specific chat room, ordered by send time.

Parameters

chatRoomId
Int!
required
The ID of the chat room to fetch messages from

Return Type

[MessageGraph!]! - An array of message objects ordered by sentAt (ascending)

GraphQL Schema

type Query {
  getMessages(chatRoomId: Int!): [MessageGraph!]!
}

type MessageGraph {
  id: Int!
  sentAt: DateTime!
  senderId: String!
  chatRoomId: Int!
  content: MessageContent!
}

union MessageContent = TextMessageContentGraph | ImageMessageContentGraph

type TextMessageContentGraph {
  text: String!
}

Example Query

query GetMessages {
  getMessages(chatRoomId: 1) {
    id
    senderId
    sentAt
    chatRoomId
    content {
      ... on TextMessageContentGraph {
        text
      }
    }
  }
}

Example Response

{
  "data": {
    "getMessages": [
      {
        "id": 101,
        "senderId": "user123",
        "sentAt": "2026-03-05T10:30:00Z",
        "chatRoomId": 1,
        "content": {
          "text": "Hello everyone!"
        }
      },
      {
        "id": 102,
        "senderId": "user456",
        "sentAt": "2026-03-05T10:32:00Z",
        "chatRoomId": 1,
        "content": {
          "text": "Hi there!"
        }
      },
      {
        "id": 103,
        "senderId": "user789",
        "sentAt": "2026-03-05T10:35:00Z",
        "chatRoomId": 1,
        "content": {
          "text": "How's everyone doing?"
        }
      }
    ]
  }
}
Messages are automatically ordered by sentAt in ascending order (oldest first).

GetFilteredMessages

Search for messages containing specific text within a chat room. This uses PostgreSQL’s trigram similarity for efficient text search.

Parameters

chatRoomId
Int!
required
The ID of the chat room to search within
searchString
String!
required
The text to search for in message content

Return Type

[MessageGraph!]! - An array of matching message objects ordered by sentAt

GraphQL Schema

type Query {
  getFilteredMessages(chatRoomId: Int!, searchString: String!): [MessageGraph!]!
}

type MessageGraph {
  id: Int!
  sentAt: DateTime!
  senderId: String!
  chatRoomId: Int!
  content: MessageContent!
}

Example Query

query SearchMessages {
  getFilteredMessages(chatRoomId: 1, searchString: "hello") {
    id
    senderId
    sentAt
    content {
      ... on TextMessageContentGraph {
        text
      }
    }
  }
}

Example Response

{
  "data": {
    "getFilteredMessages": [
      {
        "id": 101,
        "senderId": "user123",
        "sentAt": "2026-03-05T10:30:00Z",
        "content": {
          "text": "Hello everyone!"
        }
      },
      {
        "id": 205,
        "senderId": "user999",
        "sentAt": "2026-03-05T11:15:00Z",
        "content": {
          "text": "Hello! Can anyone help me?"
        }
      }
    ]
  }
}
The search uses PostgreSQL trigram similarity for fuzzy matching, which can find similar text even with minor typos.

Types Reference

ChatRoomGraph

id
Int!
Unique identifier for the chat room
name
String!
Display name of the chat room
messages
[MessageGraph!]!
Array of all messages in the chat room

MessageGraph

id
Int!
Unique identifier for the message
sentAt
DateTime!
Timestamp when the message was sent (ISO 8601 format)
senderId
String!
Identifier of the user who sent the message
chatRoomId
Int!
ID of the chat room this message belongs to
content
MessageContent!
The message content (union type supporting text and images)

MessageContent (Union)

The MessageContent is a GraphQL union type that can be:

Source Code Reference

The queries are implemented in:
  • Infrastructure/RealtimeChat.Infrastructure.GraphQL/Queries/Query.cs
Related models:
  • Infrastructure/RealtimeChat.Infrastructure.GraphQL/Models/ChatRoomGraph.cs
  • Infrastructure/RealtimeChat.Infrastructure.GraphQL/Models/MessageGraph.cs

Build docs developers (and LLMs) love