This project is a real-time message board application that demonstrates full CRUD operations with real-time updates using GraphQL subscriptions. It consists of a Node.js server using Apollo Server and a React client with TypeScript and Vite.
- Create, Read, Update, Delete (CRUD): Perform all CRUD operations on messages.
- Real-Time Updates: Leverages GraphQL subscriptions to update the UI in real-time when messages are added, updated, or deleted.
- Modular Structure: The project is organized into separate server and client directories for better manageability.
- Server: Node.js, Apollo Server, GraphQL, WebSockets, Express
- Client: React, TypeScript, Apollo Client, Vite
- Real-Time: Subscriptions using
subscriptions-transport-ws
-
Navigate to the server directory:
cd server
-
Install dependencies:
npm install
-
Build the server:
npm run build
-
Start the server:
npm start
The server will be running on http://localhost:4000/graphql.
- **Navigate to the client directory:
cd client
2. **Install dependencies**
```bash
npm install
- Start the client:
npm run dev
The client will be running on http://localhost:5173.
You can use the GraphQL playground to test your queries, mutations, and subscriptions by navigating to http://localhost:4000/graphql in your browser.
- Fetch All Messages:
query GetMessages {
messages {
id
content
}
}
- Fetch a Specific Message by ID:
query GetMessage {
message(id: "1") {
id
content
}
}
- Add a New Message:
mutation AddMessage {
addMessage(content: "Hello, world!") {
id
content
}
}
- Update an Existing Message:
mutation UpdateMessage {
updateMessage(id: "1", content: "Updated content") {
id
content
}
}
- Delete a Message:
mutation DeleteMessage {
deleteMessage(id: "1") {
id
content
}
}
- Subscribe to Message Additions:
subscription OnMessageAdded {
messageAdded {
id
content
}
}
- Subscribe to Message Updates:
subscription OnMessageUpdated {
messageUpdated {
id
content
}
}
- Subscribe to Message Deletions:
subscription OnMessageDeleted {
messageDeleted {
id
content
}
}
-
Open GraphQL Playground: Navigate to http://localhost:4000/graphql in your browser to open the GraphQL playground.
-
Run Queries: Copy the query you want to test and paste it into the left panel of the playground. Click the play button to run the query and see the results.
-
Run Mutations: Similar to queries, copy the mutation you want to test, paste it into the left panel, and click the play button to run the mutation and see the results.
-
Run Subscriptions: For subscriptions, open a new tab in the playground (using the plus button at the top). Paste the subscription you want to test into the left panel and click the play button. The subscription will stay open and listen for real-time updates. You can open another tab to run a mutation that triggers the subscription and see the results in real-time.
Run the AddMessage mutation to add a new message.
Run the GetMessages query to fetch the list of all messages, including the newly added one.
Run the UpdateMessage mutation to update the content of an existing message.
Open a subscription tab and run the OnMessageAdded subscription. Then, run the AddMessage mutation in another tab to see the subscription pick up the new message in real-time.
This provides a comprehensive guide for setting up, running, and testing both the server and client sides of your real-time message board application. It includes detailed instructions on how to install dependencies, start the development servers, and perform CRUD operations with real-time updates.