๐Ÿ“ฆ

Download nodejs-lab

12 KB โ€” Next.js 15 + TypeScript + Prisma + GraphQL Yoga + scripts + README + 10 tasks

โฌ‡ Download .tar.gz

Quick Start

# Download and extract
tar xzf nodejs-lab.tar.gz && cd nodejs-lab

# Install, setup DB, seed data
npm run setup

# Start dev server
npm run dev

# Test REST API
bash scripts/rest-examples.sh

# Test GraphQL
bash scripts/graphql-examples.sh

Tech Stack

  • Next.js 15 โ€” App Router, API routes, server components
  • TypeScript โ€” full type safety
  • Prisma โ€” ORM with SQLite (no external DB needed)
  • GraphQL Yoga โ€” GraphQL server with built-in subscriptions support

What's Inside

nodejs-lab/
โ”œโ”€โ”€ README.md โ€” Setup guide
โ”œโ”€โ”€ TASKS.md โ€” 10 practice challenges
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ prisma/
โ”‚ โ””โ”€โ”€ schema.prisma โ€” User, Post, Comment models
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ app/api/
โ”‚ โ”‚ โ”œโ”€โ”€ users/route.ts โ€” GET/POST /api/users
โ”‚ โ”‚ โ”œโ”€โ”€ users/[id]/route.ts โ€” GET/PUT/DELETE
โ”‚ โ”‚ โ”œโ”€โ”€ posts/route.ts โ€” GET/POST + filtering
โ”‚ โ”‚ โ”œโ”€โ”€ comments/route.ts
โ”‚ โ”‚ โ””โ”€โ”€ graphql/route.ts โ€” GraphQL endpoint
โ”‚ โ”œโ”€โ”€ graphql/
โ”‚ โ”‚ โ”œโ”€โ”€ schema.ts โ€” Type definitions
โ”‚ โ”‚ โ””โ”€โ”€ resolvers.ts โ€” Query/Mutation resolvers
โ”‚ โ””โ”€โ”€ lib/
โ”‚ โ””โ”€โ”€ prisma.ts โ€” Prisma client singleton
โ””โ”€โ”€ scripts/
โ”œโ”€โ”€ seed.ts โ€” 5 users, 10 posts, 20 comments
โ”œโ”€โ”€ rest-examples.sh โ€” cURL for REST
โ””โ”€โ”€ graphql-examples.sh โ€” cURL for GraphQL

API Endpoints

REST

MethodEndpointDescription
GET/api/usersList all users
POST/api/usersCreate user
GET/PUT/DELETE/api/users/:idGet/update/delete user
GET/api/posts?authorId=X&published=trueList posts with filters
POST/api/postsCreate post
GET/PUT/DELETE/api/posts/:idGet/update/delete post
GET/POST/api/commentsList/create comments
GET/DELETE/api/comments/:idGet/delete comment

GraphQL

Endpoint: POST /api/graphql

Queries: users, user(id), posts, post(id), comments

Mutations: createUser, updateUser, deleteUser, createPost, updatePost, deletePost, createComment, deleteComment

10 Practice Tasks

1. Cursor-Based Pagination

Add pagination to GET /api/posts

Accept cursor and take params. Return { data, nextCursor }. Add to GraphQL too.

2. Rate Limiting

IP-based rate limiting middleware

100 requests/minute per IP. Return 429 with Retry-After header. Add X-RateLimit headers.

3. JWT Authentication

Add login/register with JWT

POST /api/auth/register and /api/auth/login. Protect POST/PUT/DELETE routes. Add me query to GraphQL.

4. Input Validation with Zod

Validate all inputs

Add Zod schemas to all REST routes and GraphQL mutations. Return structured 400 errors with field details.

5. Centralized Error Handling

Consistent error responses

Custom error classes: NotFoundError, ValidationError, AuthError. Handle Prisma errors (P2002, P2025).

6. Fix the N+1 Problem

DataLoader for nested queries

Add posts field to User type in GraphQL. The N+1 issue appears. Fix with DataLoader batch loading.

7. Response Caching

Cache GET /api/posts

In-memory or Redis cache. Cache key includes query params. Invalidate on create/update/delete. Add X-Cache header.

8. File Upload

Avatar upload for users

POST /api/users/:id/avatar. Store in public/uploads/. Validate type (JPEG/PNG) and size (max 2MB).

9. GraphQL Subscriptions

Real-time updates

Add postCreated and commentAdded(postId) subscriptions. SSE transport via graphql-yoga.

10. Integration Testing

Jest + Supertest

Test REST CRUD, GraphQL queries/mutations, error cases. Separate test database. At least 5 tests.

Related