Skip to content

Commit 4f1e161

Browse files
Update README in GraphQL API with Node.js and TypeScript
1 parent a09d5ac commit 4f1e161

File tree

1 file changed

+79
-24
lines changed
  • GraphQL API with Node.js and TypeScript

1 file changed

+79
-24
lines changed

GraphQL API with Node.js and TypeScript/README.md

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A fully-typed GraphQL API built with Node.js, Apollo Server, MySQL, Sequelize OR
1616
-**Hot Reload** - Development mode with auto-restart
1717
-**Database ORM** - Sequelize with MySQL
1818
-**GraphQL Playground** - Built-in Apollo Studio Explorer
19-
-**Sample Data** - Pre-populated books and users
19+
-**Sample Data** - Pre-populated books, users, and authors
2020
-**Environment Config** - dotenv for configuration
2121
-**Code Quality** - ESLint and Prettier configured
2222

@@ -78,20 +78,30 @@ Visit http://localhost:4000 to explore the API!
7878
your-project/
7979
├── src/
8080
│ ├── server.ts # Application entry point
81-
│ ├── types/
82-
│ │ └── index.ts # Custom type definitions
8381
│ ├── models/
84-
│ │ ├── index.ts # Database configuration
82+
│ │ ├── index.ts # Database configuration & initialization
8583
│ │ ├── Book.ts # Book model definition
86-
│ │ └── User.ts # User model definition
84+
│ │ ├── User.ts # User model definition
85+
│ │ └── Author.ts # Author model definition
8786
│ ├── graphql/
88-
│ │ ├── schema.ts # GraphQL schema definitions
87+
│ │ ├── index.ts # GraphQL exports aggregation
88+
│ │ ├── schema/
89+
│ │ │ ├── index.ts # Schema aggregation
90+
│ │ │ ├── common.graphql # Common type definitions
91+
│ │ │ ├── book.graphql # Book schema
92+
│ │ │ ├── user.graphql # User schema
93+
│ │ │ └── author.graphql # Author schema
8994
│ │ └── resolvers/
90-
│ │ ├── index.ts # Resolver aggregation
91-
│ │ ├── bookResolvers.ts # Book queries & mutations
92-
│ │ └── userResolvers.ts # User queries & mutations
93-
│ └── generated/
94-
│ └── graphql.ts # Auto-generated types (do not edit)
95+
│ │ ├── index.ts # Resolver aggregation
96+
│ │ ├── book.resolvers.ts # Book queries & mutations
97+
│ │ ├── user.resolvers.ts # User queries & mutations
98+
│ │ └── author.resolvers.ts # Author queries & mutations
99+
│ ├── generated/
100+
│ │ └── graphql.ts # Auto-generated types (do not edit)
101+
│ └── __tests__/ # Test files
102+
│ ├── models/ # Model tests
103+
│ ├── resolvers/ # Resolver tests
104+
│ └── helpers/ # Test helper utilities
95105
├── .vscode/ # VS Code settings
96106
├── .env.example # Environment variables template
97107
├── .gitignore
@@ -214,10 +224,14 @@ query {
214224
books {
215225
id
216226
title
217-
author
218227
year
219228
created_at
220229
updated_at
230+
author {
231+
id
232+
bio
233+
website
234+
}
221235
}
222236
}
223237
```
@@ -229,8 +243,12 @@ query {
229243
book(id: "1") {
230244
id
231245
title
232-
author
233246
year
247+
author {
248+
id
249+
bio
250+
website
251+
}
234252
}
235253
}
236254
```
@@ -242,7 +260,11 @@ query {
242260
searchBooks(title: "gatsby") {
243261
id
244262
title
245-
author
263+
author {
264+
id
265+
bio
266+
website
267+
}
246268
}
247269
}
248270
```
@@ -259,13 +281,29 @@ query {
259281
}
260282
```
261283

284+
**Get all authors:**
285+
286+
```graphql
287+
query {
288+
authors {
289+
id
290+
bio
291+
website
292+
user {
293+
name
294+
email
295+
}
296+
}
297+
}
298+
```
299+
262300
### Mutations
263301

264302
**Add a book:**
265303

266304
```graphql
267305
mutation {
268-
addBook(title: "The Hobbit", author: "J.R.R. Tolkien", year: 1937) {
306+
addBook(title: "The Hobbit", author_id: 1, year: 1937) {
269307
id
270308
title
271309
author
@@ -331,7 +369,7 @@ mutation {
331369

332370
- `id` - Integer, Primary Key
333371
- `title` - String (required)
334-
- `author` - String (required)
372+
- `author_id` - Integer (required, Foreign Key to Author)
335373
- `year` - Integer (required)
336374
- `created_at` - Timestamp
337375
- `updated_at` - Timestamp
@@ -344,13 +382,22 @@ mutation {
344382
- `created_at` - Timestamp
345383
- `updated_at` - Timestamp
346384

385+
### Author
386+
387+
- `id` - Integer, Primary Key
388+
- `user_id` - Integer (required, unique, Foreign Key to User)
389+
- `bio` - Text (required)
390+
- `website` - String (optional, must be valid URL)
391+
- `created_at` - Timestamp
392+
- `updated_at` - Timestamp
393+
347394
## GraphQL Code Generator
348395

349396
This project uses GraphQL Code Generator to automatically generate TypeScript types from your GraphQL schema.
350397

351398
### How it works:
352399

353-
1. **Edit schema** in `src/graphql/schema.ts`
400+
1. **Edit schema** in `src/graphql/schema/*.graphql` files
354401
2. **Run generator**: `npm run generate`
355402
3. **Types are created** in `src/generated/graphql.ts`
356403
4. **Use typed resolvers** with full IntelliSense
@@ -365,7 +412,7 @@ This project uses GraphQL Code Generator to automatically generate TypeScript ty
365412

366413
### Adding New Types:
367414

368-
1. Update `src/graphql/schema.ts`
415+
1. Update or create `src/graphql/schema/*.graphql` files
369416
2. Run `npm run generate`
370417
3. Create resolver functions with auto-generated types
371418
4. Types automatically match your schema!
@@ -374,7 +421,14 @@ This project uses GraphQL Code Generator to automatically generate TypeScript ty
374421

375422
### Working with GraphQL Schema
376423

377-
When you modify the GraphQL schema in `src/graphql/schema.ts`:
424+
The GraphQL schema is modularized into separate `.graphql` files in `src/graphql/schema/`:
425+
426+
- `common.graphql` - Common type definitions
427+
- `book.graphql` - Book type, queries, and mutations
428+
- `user.graphql` - User type, queries, and mutations
429+
- `author.graphql` - Author type, queries, and mutations
430+
431+
When you modify any `.graphql` schema file:
378432

379433
1. Restart the dev server: `Ctrl+C` then `npm run dev`
380434
2. Or manually regenerate types: `npm run generate`
@@ -418,11 +472,12 @@ export const bookQueries: QueryResolvers = {
418472

419473
### Adding a New Model:
420474

421-
1. **Create model**: `src/models/Author.ts`
422-
2. **Update schema**: Add to `src/graphql/schema.ts`
423-
3. **Generate types**: `npm run generate`
424-
4. **Create resolvers**: `src/graphql/resolvers/authorResolvers.ts`
425-
5. **Combine resolvers**: Update `src/graphql/resolvers/index.ts`
475+
1. **Create model**: `src/models/YourModel.ts`
476+
2. **Initialize model**: Update `src/models/index.ts` to import and initialize your model
477+
3. **Create schema**: Add `src/graphql/schema/yourmodel.graphql` with types, queries, and mutations
478+
4. **Generate types**: `npm run generate`
479+
5. **Create resolvers**: `src/graphql/resolvers/yourmodel.resolvers.ts`
480+
6. **Combine resolvers**: Update `src/graphql/resolvers/index.ts`
426481

427482
TypeScript will guide you with full type checking!
428483

0 commit comments

Comments
 (0)