Skip to content

Intelli Casino GraqphQL server. Express app that's meant to run Apollo and WebSocket servers to handle GQL resolvers in service for Intelli Casino Next.js application

Notifications You must be signed in to change notification settings

dmitryjum/intelli-casino-gql-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Intelli Casino GraphQL Server

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.


Features

  • 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

Tech Stack


Project Structure

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

Getting Started (Local Development)

  1. Clone the repository:

    git clone https://github.com/dmitryjum/intelli-casino-gql-server.git
    cd intelli-casino-gql-server
  2. Install dependencies:

    npm install
  3. 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
  4. 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.

  5. Run the development server:

    npm run dev

    By default, the server starts on localhost:4000 (or whatever you set in PORT).

  6. Test it out:


Environment Variables

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

Usage Notes

  • 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 for REDIS_HOST, REDIS_PORT, and REDIS_PASSWORD.

  • Authentication
    The server uses getServerSession 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).
  • Subscriptions
    Subscriptions are handled via graphql-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.
  • CORS
    By default, the code sets CORS to allow CLIENT_ORIGIN. If your front end is served from https://myapp.vercel.app, set CLIENT_ORIGIN=https://myapp.vercel.app in production.


Running with Docker

  1. Create a Docker-specific environment file named .env.docker in the project root. This file should contain your production-style environment variables that use host.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
    
  2. Build the Docker image:

    docker build -t intelli-casino-express .
    
  3. 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


Contributing

  1. Fork this repo.
  2. Create a feature branch.
  3. Commit and push your changes.
  4. Submit a Pull Request for review.


Author


Thanks for checking out My GraphQL Server!

About

Intelli Casino GraqphQL server. Express app that's meant to run Apollo and WebSocket servers to handle GQL resolvers in service for Intelli Casino Next.js application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published