A fully-typed GraphQL API built with Node.js, Apollo Server, MySQL, Sequelize ORM, and TypeScript with auto-generated types.
- ✅ Fully Type-Safe - Auto-generated types from GraphQL schema
- ✅ Hot Reload - Development mode with auto-restart
- ✅ Database ORM - Sequelize with MySQL
- ✅ GraphQL Playground - Built-in Apollo Studio Explorer
- ✅ Sample Data - Pre-populated books and users
- ✅ Environment Config - dotenv for configuration
- ✅ Code Quality - ESLint and Prettier configured
- Quick Start
- Tech Stack
- Project Structure
- Prerequisites
- Installation
- Available Scripts
- Usage
- API Examples
- Models
- GraphQL Code Generator
- Development
- Adding New Features
- Troubleshooting
- Contributing
- License
# Clone the repository
git clone https://github.com/ganbold-adilbish/My-Code-Examples.git
cd "My-Code-Examples/GraphQL API with Node.js and TypeScript"
# Install dependencies
npm install
# Set up environment variables
cp .env.example .env
# Edit .env with your MySQL credentials
# Create database
mysql -u root -p -e "CREATE DATABASE graphql_db;"
# Start development server (generates types automatically)
npm run devVisit http://localhost:4000 to explore the API!
- Node.js
- TypeScript
- Apollo Server (GraphQL)
- Sequelize ORM
- MySQL
- GraphQL Code Generator
- dotenv
- ESLint & Prettier (Flat Config)
your-project/
├── src/
│ ├── server.ts # Application entry point
│ ├── types/
│ │ └── index.ts # Custom type definitions
│ ├── models/
│ │ ├── index.ts # Database configuration
│ │ ├── Book.ts # Book model definition
│ │ └── User.ts # User model definition
│ ├── graphql/
│ │ ├── schema.ts # GraphQL schema definitions
│ │ └── resolvers/
│ │ ├── index.ts # Resolver aggregation
│ │ ├── bookResolvers.ts # Book queries & mutations
│ │ └── userResolvers.ts # User queries & mutations
│ └── generated/
│ └── graphql.ts # Auto-generated types (do not edit)
├── .vscode/ # VS Code settings
├── .env.example # Environment variables template
├── .gitignore
├── .nvmrc # Node version specification
├── eslint.config.mts # ESLint configuration (flat config)
├── .prettierrc # Prettier configuration
├── .prettierignore # Prettier ignore patterns
├── codegen.yml # GraphQL Code Generator configuration
├── jest.config.ts # Jest testing configuration
├── package.json # Project dependencies and scripts
├── tsconfig.json # TypeScript compiler configuration
└── README.md # Project documentation
git clone https://github.com/ganbold-adilbish/My-Code-Examples.git
cd "My-Code-Examples/GraphQL API with Node.js and TypeScript"npm installCreate a .env file in the root directory:
cp .env.example .envEdit .env with your MySQL credentials:
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=your_password_here
DB_NAME=graphql_db
PORT=4000.env file to Git. It's already in .gitignore.
mysql -u root -p -e "CREATE DATABASE graphql_db;"Or using MySQL Workbench or any MySQL client.
npm run devThe server will:
- Generate TypeScript types from GraphQL schema
- Connect to MySQL database
- Sync database tables automatically
- Insert sample data
- Start the development server with hot reload
You should see:
✅ Database connected successfully
✅ Database synchronized successfully
✅ Sample books inserted
✅ Sample users inserted
🚀 GraphQL Server ready at: http://localhost:4000/
Visit http://localhost:4000 to explore the API with Apollo Studio Explorer!
| Command | Description |
|---|---|
npm run generate |
Generate TypeScript types from GraphQL schema |
npm run build |
Generate types + compile TypeScript to JavaScript |
npm start |
Run production server from compiled code |
npm run dev |
Run development server with hot reload |
npm run watch |
Watch TypeScript compilation in real-time |
npm test |
Run Jest tests |
npm run test:watch |
Run Jest tests in watch mode |
npm run test:coverage |
Run tests with coverage report |
npm run format |
Format code with Prettier |
npm run format:check |
Check if code is formatted correctly |
npm run lint |
Run ESLint to check code quality |
npm run lint:fix |
Run ESLint and auto-fix issues |
npm run check |
Run both lint and format checks |
Open your browser and go to http://localhost:4000 to access Apollo Studio Explorer.
Get all books:
query {
books {
id
title
author
year
created_at
updated_at
}
}Get a specific book:
query {
book(id: "1") {
id
title
author
year
}
}Search books:
query {
searchBooks(title: "gatsby") {
id
title
author
}
}Get all users:
query {
users {
id
name
email
}
}Add a book:
mutation {
addBook(title: "The Hobbit", author: "J.R.R. Tolkien", year: 1937) {
id
title
author
}
}Update a book:
mutation {
updateBook(id: "1", year: 1950) {
id
title
year
}
}Delete a book:
mutation {
deleteBook(id: "3")
}Add a user:
mutation {
addUser(name: "Charlie Brown", email: "charlie@example.com") {
id
name
email
}
}Update a user:
mutation {
updateUser(id: "1", name: "Alice Smith") {
id
name
email
}
}Delete a user:
mutation {
deleteUser(id: "2")
}id- Integer, Primary Keytitle- String (required)author- String (required)year- Integer (required)created_at- Timestampupdated_at- Timestamp
id- Integer, Primary Keyname- String (required)email- String (required, unique)created_at- Timestampupdated_at- Timestamp
This project uses GraphQL Code Generator to automatically generate TypeScript types from your GraphQL schema.
- Edit schema in
src/graphql/schema.ts - Run generator:
npm run generate - Types are created in
src/generated/graphql.ts - Use typed resolvers with full IntelliSense
- ✅ Automatic type generation from GraphQL schema
- ✅ Type-safe resolvers
- ✅ Full IDE autocomplete
- ✅ Catch errors at compile time
- ✅ Single source of truth (GraphQL schema)
- Update
src/graphql/schema.ts - Run
npm run generate - Create resolver functions with auto-generated types
- Types automatically match your schema!
When you modify the GraphQL schema in src/graphql/schema.ts:
- Restart the dev server:
Ctrl+Cthennpm run dev - Or manually regenerate types:
npm run generate
The dev server will automatically pick up the new types and restart.
Run the check script to ensure code quality:
npm run checkThis runs both ESLint and Prettier checks.
npm test # Run once
npm run test:watch # Watch mode
npm run test:coverage # With coverage reportAll resolvers are fully typed using auto-generated types:
import { QueryResolvers, MutationResolvers } from '../../generated/graphql';
export const bookQueries: QueryResolvers = {
books: async () => {
// Return type is automatically inferred!
return await BookModel.findAll();
},
};- Create model:
src/models/Author.ts - Update schema: Add to
src/graphql/schema.ts - Generate types:
npm run generate - Create resolvers:
src/graphql/resolvers/authorResolvers.ts - Combine resolvers: Update
src/graphql/resolvers/index.ts
TypeScript will guide you with full type checking!
If you see "Access denied for user":
- Check your MySQL credentials in
.env - Ensure MySQL is running:
mysql.server status(macOS) orsudo service mysql status(Linux) - Verify the database exists:
mysql -u root -p -e "SHOW DATABASES;"
If port 4000 is taken:
- Change
PORTin your.envfile - Or kill the process using port 4000:
lsof -ti:4000 | xargs kill
If npm run generate fails:
# Clear generated files and try again
rm -rf src/generated
npm run generateIf you get module import errors:
# Clear node_modules and reinstall
rm -rf node_modules package-lock.json
npm installIf linting or formatting fails:
# Auto-fix ESLint issues
npm run lint -- --fix
# Format all files
npm run formatContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project includes VS Code settings for:
- Format on save
- ESLint auto-fix on save
Recommended VS Code extensions:
- ESLint (
dbaeumer.vscode-eslint) - Prettier (
esbenp.prettier-vscode)
MIT