This is a standalone Express + Apollo Server application that provides a GraphQL endpoint with subscriptions (via WebSockets). It also integrates with Prisma for database access and uses NextAuth logic to authenticate requests with session cookies.
- GraphQL endpoint at
/api/graphql
- Subscriptions via WebSockets (using
graphql-ws
) - Redis PubSub for handling real-time subscriptions
- Prisma ORM for PostgreSQL (or any other supported DB)
- NextAuth-compatible session validation
- Custom resolvers, mutations, and subscriptions for real-time quiz/gaming logic
- TypeScript
- Express
- Apollo Server
- GraphQL
- graphql-ws for subscriptions
- Redis for PubSub
- Prisma for database access
- NextAuth for session handling
- WebSockets
my-graphql-server/
┣ prisma/
┃ ┗ schema.prisma # Prisma schema
┣ src/
┃ ┣ resolvers/
┃ ┃ ┗ index.ts # Your GraphQL resolvers (Queries, Mutations, Subscriptions)
┃ ┣ schema.ts # GraphQL type definitions (SDL)
┃ ┣ lib/
┃ ┃ ┗ redisPubSub.ts # Redis PubSub configuration
┃ ┣ nextAuthOptions.ts # (Optional) NextAuth config if you want to replicate it here
┃ ┗ index.ts # Entry point (Express + ApolloServer setup)
┣ .env.example # Example environment variables
┣ package.json
┣ tsconfig.json
┗ README.md # This file
-
Clone the repository:
git clone https://github.com/dmitryjum/intelli-casino-gql-server.git cd intelli-casino-gql-server
-
Install dependencies:
npm install
-
Create a
.env
file in the project root (or wherever you like) based on.env.example
. For example:cp .env.example .env
Then update the variables inside
.env
:# .env DATABASE_URL="postgresql://user:password@localhost:5432/mydb" NEXTAUTH_SECRET="some-random-secret" CLIENT_ORIGIN="http://localhost:3000" PORT=4000
-
Set up Prisma (optional step if you need migrations):
npm run prisma:migrate npm run prisma:generate
This will apply migrations and generate Prisma client files.
-
Run the development server:
npm run dev
By default, the server starts on
localhost:4000
(or whatever you set inPORT
). -
Test it out:
- Visit
http://localhost:4000/api/graphql
in your browser or use a tool like Apollo Sandbox to send GraphQL queries. - Subscriptions should be available via
ws://localhost:4000/api/graphql
.
- Visit
Variable | Description | Example |
---|---|---|
DATABASE_URL |
The URL for your Postgres (or other) database | postgresql://user:pass@host:5432/db |
NEXTAUTH_SECRET |
Secret key for NextAuth session encryption/validation | my-secret-key |
CLIENT_ORIGIN |
The origin (URL) of your front-end (CORS allowed) | http://localhost:3000 |
PORT |
Port on which the server will listen | 4000 |
REDIS_HOST |
Host for your Redis server | 127.0.0.1 |
REDIS_PORT |
Port for your Redis server | 6379 |
REDIS_PASSWORD |
Password for your Redis server (if any) | your-redis-password |
-
Redis PubSub
The server uses Redis for handling real-time subscriptions. Ensure your Redis server is running and accessible with the correct environment variables set forREDIS_HOST
,REDIS_PORT
, andREDIS_PASSWORD
. -
Authentication
The server usesgetServerSession
from NextAuth to validate the user’s session cookie. Make sure you have:- The same
NEXTAUTH_SECRET
across both this server and your Next.js app. - The same or compatible session storage (e.g., the same
DATABASE_URL
if you store sessions in the DB).
- The same
-
Subscriptions
Subscriptions are handled viagraphql-ws
and Redis PubSub.- For WebSocket connections, the path is
ws://<HOST>:<PORT>/api/graphql
. - Make sure your front-end Apollo Client is configured with a
WebSocketLink
to that URL.
- For WebSocket connections, the path is
-
CORS
By default, the code sets CORS to allowCLIENT_ORIGIN
. If your front end is served fromhttps://myapp.vercel.app
, setCLIENT_ORIGIN=https://myapp.vercel.app
in production.
-
Create a Docker-specific environment file named
.env.docker
in the project root. This file should contain your production-style environment variables that usehost.docker.internal
for local services. For example:# .env.docker DATABASE_URL=postgresql://user:[email protected]:5432/intelli_casino?schema=public REDIS_HOST=host.docker.internal
-
Build the Docker image:
docker build -t intelli-casino-express .
-
Run the Docker container and inject the Docker environment file:
docker run -b 4000:4000 --env-file .env.docker intelli-casino-express
This setup ensures that your application running insde Docker connects to your local PostgreSQL and Redis instances via host.docker.internal
- Fork this repo.
- Create a feature branch.
- Commit and push your changes.
- Submit a Pull Request for review.
- Dmitry Jum – @dmitryjum
Thanks for checking out My GraphQL Server!