Overview
The GraphQL layer exposes PriceSignal’s functionality through a type-safe GraphQL API using HotChocolate 14. It provides queries, mutations, and real-time subscriptions for cryptocurrency price tracking and alerts.Architecture
Type System
PriceSignal uses HotChocolate’s annotation-based approach with explicit field binding:- Explicit control over exposed fields
- Rename fields (EntityId → id)
- Type safety with compile-time checks
- Hide internal implementation details
Queries
InstrumentQueries
Query instruments (trading pairs) with filtering, sorting, and pagination.- Pagination: Cursor-based with total count
- Filtering: Dynamic WHERE clauses
- Sorting: Multi-field ordering
- Projection: Only fetch requested fields (N+1 optimization)
PriceRuleQueries
Query user’s price alert rules with authentication.- Filters by authenticated user ID
- Prevents unauthorized access to other users’ rules
Mutations
PriceRuleMutations
Create, update, delete, and toggle price alert rules.- Validate user authentication
- Validate referenced entities exist
- Parse JSON condition metadata
- Save to database
- Update in-memory cache
- Trigger subscription updates for background service
Subscriptions
PriceSubscriptions
Real-time price updates via GraphQL subscriptions (WebSockets).Complex Types
PriceRuleType
Defines GraphQL schema for PriceRule with DataLoaders for N+1 prevention.DataLoaders
Batch and cache database queries to solve N+1 problems.- Batches multiple queries into one
- Caches results within request
- Prevents N+1 query problems
- Automatic with HotChocolate
Input Types
PriceRuleInput
Input object for creating/updating price rules.Filtering and Sorting
Custom Filter Types
Performance Optimizations
Projection
Only fetches requested fields from database:Pagination
Cursor-based pagination for large datasets:DataLoader Batching
Automatic query batching:- 10 price rules each with conditions
- Without DataLoader: 1 + 10 queries (N+1)
- With DataLoader: 2 queries (batched)
Error Handling
GraphQL errors with proper structure:Authentication
Injected IUser service provides current user context:Next Steps
- See Domain Layer for entity definitions
- Learn Application Layer business logic
- Explore Infrastructure Layer for persistence