Skip to content

Commit a14fb2b

Browse files
Merge pull request #1 from FullstackCodingGuy/graphql-implementation
Graphql implementation
2 parents 3ac3049 + b9921bc commit a14fb2b

40 files changed

+6006
-47
lines changed

README.md

Lines changed: 260 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,38 @@ This project is a boilerplate for building .NET API applications with various fe
88

99
## Features
1010

11-
- [ ] [Vertical Slicing Architecture](https://github.com/FullstackCodingGuy/Developer-Fundamentals/wiki/Architecture-%E2%80%90-Vertical-Slicing-Architecture)
11+
- [x] [Vertical Slicing Architecture](https://github.com/FullstackCodingGuy/Developer-Fundamentals/wiki/Architecture-%E2%80%90-Vertical-Slicing-Architecture)
1212
- [x] Swagger
1313
- [x] Minimal API
14+
- [x] **GraphQL API with HotChocolate**
15+
- [x] Professional architecture with clean separation
16+
- [x] Complete blog management domain (Blogs, Posts, Comments)
17+
- [x] DataLoaders for N+1 query prevention
18+
- [x] Real-time subscriptions with Redis
19+
- [x] Built-in GraphQL Playground and Schema explorer
20+
- [x] Query complexity analysis and rate limiting
21+
- [x] Field-level authorization support
22+
- [x] Comprehensive error handling
23+
- [x] Performance monitoring and observability
1424
- [x] Authentication using JWT Bearer tokens
15-
- [ ] Authorization
25+
- [x] Authorization with role-based access control
1626
- [x] Rate limiting to prevent API abuse
1727
- [x] CORS policies for secure cross-origin requests
1828
- [x] Response caching and compression
1929
- [x] Logging with Serilog
2030
- [x] Health check endpoint
2131
- [x] [Middlewares](https://github.com/FullstackCodingGuy/dotnetapi-boilerplate/tree/main/src/Middlewares)
22-
- [ ] Entity Framework
23-
- [ ] Serilog
24-
- [ ] FluentValidation
32+
- [x] Entity Framework Core with SQLite
33+
- [x] Serilog with structured logging
34+
- [x] FluentValidation
2535
- [ ] Vault Integration
2636
- [ ] MQ Integration
27-
- [ ] Application Resiliency
28-
- [ ] Performance
29-
- [ ] Response Compression
30-
- [ ] Response Caching
31-
- [ ] Metrics
37+
- [x] Application Resiliency (GraphQL level)
38+
- [x] Performance
39+
- [x] Response Compression
40+
- [x] Response Caching
41+
- [x] GraphQL query optimization
42+
- [x] DataLoaders for efficient data fetching
3243
- [ ] Deployment
3344
- [ ] Docker
3445
- [ ] Podman
@@ -98,13 +109,251 @@ docker-compose down
98109
99110
The application includes a health check endpoint to verify that the API is running. You can access it at:
100111
101-
102112
```
103113
GET /health
104114

105115
This will return a simple "Healthy" message.
106116
```
107117
118+
## GraphQL API
119+
120+
This boilerplate includes a comprehensive GraphQL implementation using HotChocolate, designed with professional architecture patterns and enterprise-grade features.
121+
122+
### GraphQL Features
123+
124+
#### 🏗️ **Professional Architecture**
125+
- **Clean Architecture**: Vertical slicing with clear separation of concerns
126+
- **Domain-Driven Design**: Blog management domain with Blogs, Posts, and Comments
127+
- **Repository Pattern**: Abstracted data access with Entity Framework Core
128+
- **Service Layer**: Business logic separation with comprehensive services
129+
130+
#### 🚀 **Core GraphQL Capabilities**
131+
- **Complete CRUD Operations**: Full Create, Read, Update, Delete support
132+
- **Advanced Querying**: Complex filtering, sorting, and pagination
133+
- **Real-time Subscriptions**: Live updates using Redis as message broker
134+
- **Field-level Authorization**: Granular security control
135+
- **Input Validation**: Comprehensive data validation and sanitization
136+
137+
#### ⚡ **Performance Optimization**
138+
- **DataLoaders**: Automatic N+1 query prevention with batch loading
139+
- **Query Complexity Analysis**: Protection against expensive queries
140+
- **Response Caching**: Intelligent caching strategies
141+
- **Database Optimization**: Efficient EF Core queries with projections
142+
143+
#### 🔐 **Security & Observability**
144+
- **JWT Authentication**: Seamless integration with existing auth
145+
- **Role-based Authorization**: Fine-grained access control
146+
- **Rate Limiting**: GraphQL-specific rate limiting
147+
- **Comprehensive Logging**: Structured logging with Serilog
148+
- **Error Handling**: Professional error responses with detailed context
149+
150+
#### 🛠️ **Developer Experience**
151+
- **Built-in GraphQL Playground**: Interactive query interface at `/graphql`
152+
- **Schema Explorer**: Full schema documentation and introspection
153+
- **Type Safety**: Strongly typed resolvers and models
154+
- **Extensible Design**: Easy to add new types and features
155+
156+
### GraphQL Endpoints
157+
158+
#### **Main GraphQL Endpoint**
159+
```
160+
POST /graphql
161+
```
162+
- Primary endpoint for all GraphQL operations
163+
- Supports queries, mutations, and subscriptions
164+
- Built-in GraphQL Playground available in development
165+
166+
#### **GraphQL Playground** (Development)
167+
```
168+
GET /graphql
169+
```
170+
- Interactive GraphQL IDE
171+
- Schema exploration and documentation
172+
- Query testing and validation
173+
- Real-time subscription testing
174+
175+
### Example Queries
176+
177+
#### **Get All Blogs with Posts**
178+
```graphql
179+
query GetBlogsWithPosts {
180+
blogs {
181+
id
182+
name
183+
description
184+
posts {
185+
id
186+
title
187+
content
188+
author {
189+
name
190+
email
191+
}
192+
comments {
193+
id
194+
content
195+
author
196+
}
197+
}
198+
}
199+
}
200+
```
201+
202+
#### **Create a New Blog**
203+
```graphql
204+
mutation CreateBlog {
205+
createBlog(input: {
206+
name: "Tech Blog"
207+
description: "A blog about technology"
208+
author: "John Doe"
209+
tags: ["tech", "programming"]
210+
}) {
211+
blog {
212+
id
213+
name
214+
description
215+
author
216+
tags
217+
createdAt
218+
}
219+
errors {
220+
message
221+
}
222+
}
223+
}
224+
```
225+
226+
#### **Subscribe to New Posts**
227+
```graphql
228+
subscription NewPosts {
229+
onPostCreated {
230+
id
231+
title
232+
content
233+
blog {
234+
name
235+
}
236+
author {
237+
name
238+
}
239+
}
240+
}
241+
```
242+
243+
#### **Complex Query with Filtering**
244+
```graphql
245+
query SearchPosts {
246+
posts(
247+
where: {
248+
and: [
249+
{ title: { contains: "GraphQL" } }
250+
{ isPublished: { eq: true } }
251+
{ createdAt: { gte: "2024-01-01" } }
252+
]
253+
}
254+
order: [{ createdAt: DESC }]
255+
take: 10
256+
) {
257+
id
258+
title
259+
content
260+
blog {
261+
name
262+
}
263+
commentCount
264+
viewCount
265+
}
266+
}
267+
```
268+
269+
### Architecture Overview
270+
271+
```
272+
Features/GraphQL/
273+
├── Configuration/ # GraphQL server configuration
274+
│ └── GraphQLConfiguration.cs
275+
├── Models/ # GraphQL types and DTOs
276+
│ ├── BlogType.cs
277+
│ ├── PostType.cs
278+
│ ├── CommentType.cs
279+
│ └── AuthorType.cs
280+
├── Services/ # Business logic services
281+
│ ├── IBlogService.cs
282+
│ ├── BlogService.cs
283+
│ ├── IPostService.cs
284+
│ └── PostService.cs
285+
├── Resolvers/ # GraphQL resolvers
286+
│ ├── Query.cs # Query operations
287+
│ ├── Mutation.cs # Mutation operations
288+
│ ├── Subscription.cs # Real-time subscriptions
289+
│ └── FieldResolvers.cs # Field-level resolvers
290+
└── DataLoaders/ # Performance optimization
291+
├── BlogDataLoaders.cs
292+
└── PostDataLoaders.cs
293+
```
294+
295+
### Performance Features
296+
297+
#### **DataLoaders**
298+
Automatically batches and caches database queries to prevent N+1 problems:
299+
- `BlogByIdDataLoader`: Efficient blog loading by ID
300+
- `PostsByBlogIdDataLoader`: Batch loading of posts by blog
301+
- `CommentsByPostIdDataLoader`: Efficient comment loading
302+
303+
#### **Query Complexity Analysis**
304+
Protects against expensive queries with configurable limits:
305+
- Maximum query depth: 10 levels
306+
- Maximum query complexity: 1000 points
307+
- Field introspection limits in production
308+
309+
#### **Caching Strategy**
310+
Multi-level caching for optimal performance:
311+
- DataLoader-level caching (request scoped)
312+
- Service-level caching for expensive operations
313+
- Response caching for static data
314+
315+
### Getting Started with GraphQL
316+
317+
1. **Start the application**:
318+
```bash
319+
dotnet run
320+
```
321+
322+
2. **Open GraphQL Playground**:
323+
Navigate to `http://localhost:8000/graphql`
324+
325+
3. **Explore the Schema**:
326+
Use the schema explorer to understand available types and operations
327+
328+
4. **Try Sample Queries**:
329+
Copy and paste the example queries above
330+
331+
5. **Test Real-time Features**:
332+
Open multiple browser tabs to test subscriptions
333+
334+
### Configuration
335+
336+
GraphQL is configured in `Features/GraphQL/Configuration/GraphQLConfiguration.cs` with:
337+
338+
- **HotChocolate Server**: Latest version with all features enabled
339+
- **Entity Framework Integration**: Automatic query translation
340+
- **Redis Subscriptions**: Real-time capabilities
341+
- **Authentication Integration**: JWT token validation
342+
- **Error Handling**: Professional error responses
343+
- **Performance Monitoring**: Query execution tracking
344+
345+
### Best Practices Implemented
346+
347+
- **Single Responsibility**: Each resolver handles one concern
348+
- **Dependency Injection**: All services properly registered
349+
- **Async/Await**: Non-blocking operations throughout
350+
- **Error Boundaries**: Comprehensive error handling
351+
- **Type Safety**: Strong typing for all operations
352+
- **Documentation**: Inline documentation for all types
353+
- **Testing Ready**: Architecture supports unit and integration testing
354+
355+
This GraphQL implementation provides a solid foundation for building modern, efficient APIs with excellent developer experience and enterprise-grade performance.
356+
108357
### Logging with Serilog
109358

110359
Serilog is configured to log to the console and a file with daily rotation. You can customize the logging settings in the `serilog.json` file.

0 commit comments

Comments
 (0)