An open API service indexing awesome lists of open source software.

https://github.com/iberasoft/reactjs-graphql-practice

:apple: Simple app for learning puorposes
https://github.com/iberasoft/reactjs-graphql-practice

apollo authentication graphql nodejs prisma reactjs unit-test

Last synced: 5 months ago
JSON representation

:apple: Simple app for learning puorposes

Awesome Lists containing this project

README

          

# React GraphQL E-Commerce Platform


React GraphQL Practice

This is a full-stack e-commerce application with a modern GraphQL architecture for learning purposes. Its showcases a production-ready app including type-safe resolvers, efficient data fetching, real-time subscriptions, and comprehensive error handling.

## ๐Ÿš€ Tech Stack

### Backend
- **GraphQL API**: Apollo Server 4.x with TypeScript
- **Database**: PostgreSQL with Prisma 5.x ORM
- **Authentication**: JWT-based auth with httpOnly cookies
- **Payments**: Stripe integration
- **Real-time**: GraphQL Subscriptions

### Frontend
- **Framework**: Next.js 15 with React 18
- **GraphQL Client**: Apollo Client 3.x with React Hooks
- **Styling**: Styled Components 6.x
- **Type Safety**: TypeScript with GraphQL Code Generator

### Infrastructure
- **Containerization**: Docker & Docker Compose
- **Deployment**: Vercel (frontend), Railway/Render (backend)
- **Development**: Hot reload, type checking, linting

## ๐Ÿ“‹ Features

### GraphQL Implementation Highlights

- **Schema-First Design**: Well-defined GraphQL schema with proper type definitions
- **Efficient Resolvers**: Optimized database queries with Prisma, preventing N+1 problems
- **Type Safety**: End-to-end type safety from database to frontend using TypeScript and code generation
- **Error Handling**: Comprehensive error handling with GraphQL error extensions
- **Authentication**: Secure JWT-based authentication with permission-based authorization
- **Real-time Capabilities**: GraphQL subscriptions for live updates (cart, orders, etc.)
- **Query Optimization**: Field-level resolvers with data loader pattern
- **Input Validation**: Zod-based validation for mutations

### Application Features

- ๐Ÿ›๏ธ **Product Catalog**: Browse, search, and filter items with pagination
- ๐Ÿ›’ **Shopping Cart**: Add/remove items with real-time updates
- ๐Ÿ’ณ **Checkout**: Secure payment processing with Stripe
- ๐Ÿ‘ค **User Management**: Registration, authentication, password reset
- ๐Ÿ” **Role-Based Access**: Permission system (ADMIN, USER, ITEMCREATE, etc.)
- ๐Ÿ“ฆ **Order Management**: View order history and details
- ๐ŸŽจ **Modern UI**: Responsive design with smooth animations

## ๐Ÿ—๏ธ Architecture

### GraphQL Schema Design

The application follows a clean, modular GraphQL schema structure:

```graphql
type Query {
items(where: ItemWhereInput, orderBy: ItemOrderByInput, skip: Int, first: Int): [Item]!
item(where: ItemWhereUniqueInput!): Item
itemsConnection(where: ItemWhereInput): ItemConnection!
me: User
users: [User]!
}

type Mutation {
createItem(title: String, description: String, price: Int, image: String): Item!
updateItem(id: ID!, title: String, description: String, price: Int): Item!
deleteItem(id: ID!): Item
signup(email: String!, password: String!, name: String!): User!
signin(email: String!, password: String!): User!
addToCart(id: ID!): CartItem
removeFromCart(id: ID!): CartItem
createOrder(token: String!): Order!
# ... more mutations
}
```

### Resolver Patterns

- **Forwarding Resolvers**: Simple queries forwarded directly to Prisma
- **Custom Resolvers**: Complex business logic with proper error handling
- **Context-Based Auth**: User authentication via request context
- **Permission Checks**: Field and operation-level permission validation

### Database Schema

Prisma schema with proper relationships:

```prisma
model User {
id String @id @default(uuid())
name String
email String @unique
password String
permissions Permission[]
cart CartItem[]
orders Order[]
# ... more fields
}

model Item {
id String @id @default(uuid())
title String
description String
price Int
image String?
user User @relation(fields: [userId], references: [id])
# ... more fields
}
```

## ๐Ÿ› ๏ธ Getting Started

### Prerequisites

- Node.js 20+ and npm/yarn
- Docker and Docker Compose (for local development)
- PostgreSQL (or use Docker)
- Stripe account (for payments)

### Local Development with Docker

1. **Clone the repository**
```bash
git clone
cd reactjs-graphql-practice
```

2. **Set up environment variables**
```bash
cp backend/.env.example backend/.env
cp frontend/.env.example frontend/.env.local
```

Edit the `.env` files with your configuration:
- Database connection string
- JWT secret
- Stripe keys
- Email service credentials

3. **Start services with Docker Compose**
```bash
docker-compose up -d
```

This will start:
- PostgreSQL database
- GraphQL API server (port 4000)
- Frontend development server (port 3000)

4. **Run database migrations**
```bash
cd backend
npm run prisma:migrate
npm run prisma:generate
```

5. **Access the application**
- Frontend: http://localhost:3000
- GraphQL Playground: http://localhost:4000/graphql
- Prisma Studio: `npm run prisma:studio` (in backend directory)

### Manual Setup (without Docker)

1. **Backend Setup**
```bash
cd backend
npm install
npm run prisma:generate
npm run dev
```

2. **Frontend Setup**
```bash
cd frontend
npm install
npm run dev
```

## ๐Ÿ“š GraphQL Examples

### Query Example

```graphql
query GetItems($skip: Int, $first: Int) {
items(skip: $skip, first: $first, orderBy: createdAt_DESC) {
id
title
description
price
image
user {
name
}
}
itemsConnection {
aggregate {
count
}
}
}
```

### Mutation Example

```graphql
mutation CreateItem($title: String!, $description: String!, $price: Int!) {
createItem(
title: $title
description: $description
price: $price
) {
id
title
price
}
}
```

### Authentication Example

```graphql
mutation SignIn($email: String!, $password: String!) {
signin(email: $email, password: $password) {
id
name
email
permissions
}
}
```

## ๐Ÿงช Testing

```bash
# Backend tests
cd backend
npm test

# Frontend tests
cd frontend
npm test

# Run all tests
npm run test:all
```

## ๐Ÿšข Deployment

### Frontend (Vercel)

1. Connect your GitHub repository to Vercel
2. Configure environment variables in Vercel dashboard
3. Deploy automatically on push to main branch

### Backend (Railway/Render)

1. Connect your repository
2. Set up PostgreSQL database
3. Configure environment variables
4. Deploy

## ๐Ÿ“– GraphQL Best Practices Demonstrated

1. **Schema Design**: Clear, intuitive schema with proper naming conventions
2. **Resolver Efficiency**: Optimized queries with Prisma, avoiding N+1 problems
3. **Error Handling**: Meaningful error messages with proper error codes
4. **Type Safety**: End-to-end TypeScript types from schema to components
5. **Security**: Authentication and authorization at resolver level
6. **Performance**: Field-level resolvers, query complexity analysis
7. **Documentation**: Self-documenting schema with descriptions
8. **Testing**: Comprehensive test coverage for resolvers and components

## ๐Ÿ” Security Features

- JWT authentication with httpOnly cookies
- Password hashing with bcrypt
- Input validation and sanitization
- Permission-based access control
- CORS configuration
- SQL injection prevention (Prisma)

## ๐Ÿ“ Project Structure

```
.
โ”œโ”€โ”€ backend/ # GraphQL API server
โ”‚ โ”œโ”€โ”€ src/
โ”‚ โ”‚ โ”œโ”€โ”€ schema/ # GraphQL schema and resolvers
โ”‚ โ”‚ โ”œโ”€โ”€ lib/ # Utilities (Prisma, auth, etc.)
โ”‚ โ”‚ โ””โ”€โ”€ middleware/ # Express/Apollo middleware
โ”‚ โ”œโ”€โ”€ prisma/ # Prisma schema and migrations
โ”‚ โ””โ”€โ”€ Dockerfile
โ”œโ”€โ”€ frontend/ # Next.js application
โ”‚ โ”œโ”€โ”€ src/
โ”‚ โ”‚ โ”œโ”€โ”€ app/ # Next.js pages
โ”‚ โ”‚ โ”œโ”€โ”€ components/ # React components
โ”‚ โ”‚ โ”œโ”€โ”€ graphql/ # GraphQL queries/mutations
โ”‚ โ”‚ โ””โ”€โ”€ lib/ # Utilities (Apollo Client, etc.)
โ”‚ โ””โ”€โ”€ Dockerfile
โ”œโ”€โ”€ docker-compose.yml # Local development setup
โ””โ”€โ”€ README.md
```

## ๐Ÿค Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

## ๐ŸŽฏ Learning Resources

This project is designed as a comprehensive GraphQL learning resource, demonstrating:

- GraphQL schema design and best practices
- Apollo Server setup and configuration
- Prisma ORM integration with GraphQL
- Apollo Client usage in React applications
- Type-safe GraphQL development
- Real-world authentication and authorization
- Payment integration patterns
- Testing GraphQL APIs and React components

## ๐Ÿ“ง Contact

For questions or feedback, please open an issue on GitHub.

---

**Built with โค๏ธ using GraphQL, React, and modern web technologies**