A forum backend built with Apollo Server 5, Express 5, GraphQL 16 and Sequelize 6 on Node.js 24. It models users, channels, threads, replies and favorites, with JWT authorization, request-scoped DataLoader batching and subscription events.
The focus is a coherent API rather than a UI: ownership checks, transactional reply/status changes, repeatable database setup and executable integration scenarios. Browse the complete generated schema alongside the resolvers.
SQLite is the default in the example configuration, so the API can be explored without MySQL or cloud credentials.
git clone https://github.com/einnor/graphql-forum-api.git
cd graphql-forum-api
npm ci
cp env.example .envGenerate a signing secret with openssl rand -hex 32 and put it in .env as JWT_SECRET. Keep .env private.
npm run db:migrate
npm run db:seed
npm startThe API listens at http://127.0.0.1:4000/graphql; database readiness is available at /health. The seed command adds sample channels. No default administrator or shared password is created. Optional admin seeding requires ADMIN_EMAIL and an ADMIN_PASSWORD of at least 12 characters.
SQLite persists in the ignored forum.sqlite file. For MySQL, set DB_DIALECT=mysql and the DB_* connection variables, create an empty database, then run the same migrations. SQLite is a convenience for development, not a claim of identical locking or operational behavior across dialects.
List channels:
curl http://127.0.0.1:4000/graphql \
-H 'Content-Type: application/json' \
-d '{"query":"{ channels { id name slug } }"}'Register a local account:
mutation {
signUp(
username: "demo"
email: "demo@example.com"
password: "local-example-password"
) {
token
}
}Send the returned token as Authorization: Bearer <token> for protected operations. Tokens expire after one hour. Sign in later using signIn(email:, password:).
query {
me {
id
username
threads {
id
title
status
}
}
}
mutation {
createThread(
title: "Designing a reliable API"
content: "Where should consistency boundaries live?"
channelId: "<id from channels>"
) {
id
slug
content
}
}The schema also supports replies, per-user favorites, selecting/unselecting a best answer, profile/password changes, admin-only thread locking and optional Cloudinary avatar uploads. Thread lists use page/perPage pagination, with 1–100 results per page.
Connect a graphql-ws client to ws://127.0.0.1:4000/graphql using the graphql-transport-ws subprotocol. Connection parameters may include authorization: "Bearer <token>".
subscription {
replyAdded {
id
content
user {
username
}
}
}Reply events are global public-forum events, not private or per-thread streams. The in-memory PubSub implementation works inside one process only; a multi-instance deployment needs a shared broker and an explicit subscription authorization/filtering policy.
| Concern | Implementation and boundary |
|---|---|
| Authorization | Schema transforms enforce @auth and @isAdmin; resolvers enforce resource ownership. |
| Passwords | Bcrypt hashing runs only when a password changes, so editing a profile does not invalidate login. |
| Nested queries | DataLoaders are created per request, avoiding cache leakage between users and batching related records. |
| Answer consistency | Thread-level transactions update the selected answer and solved status together; selecting another answer clears the previous one. |
| Favorites | A composite identity and database unique index prevent duplicate user/reply pairs. |
| Async completion | Mutations await writes before responding; reply creation, answer selection and reply deletion use transactions. |
| Uploads | Optional Cloudinary integration, one file up to 10 MB; multipart clients must set Apollo-Require-Preflight: true. |
npm test
npm run schemaTests create an isolated SQLite database by running the actual migrations, start the real HTTP GraphQL server, and exercise signup/signin, authorization, profile-update/password preservation, nested relations, favorite deduplication/removal, best-answer replacement, thread locking, pagination errors and subscription event execution.
CI runs the same scenarios against SQLite and MySQL 8.4. Cloudinary uploads and production WebSocket infrastructure require separate integration checks; the in-process subscription test does not certify a deployed WebSocket service.
This refresh replaces Apollo Server 2 and its legacy schema-directive/subscription APIs. Existing clients must account for:
- The modern WebSocket subprotocol;
subscriptions-transport-wsclients are not compatible. - Explicit upload middleware and the preflight header described above.
- Nullable
User.avatar, matching accounts without an uploaded image. - Removal of the nonfunctional
Reply.titlefield (replies storecontent) and addition of queryableThread.content. - A unique-favorites migration. Back up existing databases and resolve duplicate reply/user pairs before applying it.
The legacy Replies.threadID database column is preserved with a model field mapping rather than silently renaming stored data. The optional administrator seed no longer creates a publicly known account/password.
This is a portfolio-scale API, not an operated forum service. Add request/operation complexity budgets, rate limiting, a stronger password policy, token revocation/role-refresh policy, monitoring, durable subscription delivery and deployment-specific CORS/TLS controls. JWT roles remain valid until token expiry. Validate backup/restore and migration procedures against representative data rather than treating passing local tests as a production-readiness guarantee.