Overview
The PriceSignal backend is built with:- ASP.NET Core 8.0 - Web framework
- HotChocolate 13.9 - GraphQL server implementation
- Entity Framework Core 8.0 - ORM for database access
- PostgreSQL - Primary database
- WebSocket - Real-time price updates
Project Structure
The backend follows a clean architecture pattern:PriceSignal (Web API)
Main entry point containing:- GraphQL schema and resolvers
- API endpoints
- Middleware configuration
- SPA integration
Infrastructure
Data access and external integrations:- Entity Framework Core DbContext
- Database migrations
- External API clients (Binance, Alpaca)
- WebSocket clients for real-time data
Key Dependencies
GraphQL (HotChocolate)
- HotChocolate.AspNetCore (13.9.4) - GraphQL server
- HotChocolate.AspNetCore.Authorization (13.9.4) - GraphQL authorization
- HotChocolate.Data.EntityFramework (13.9.4) - EF Core integration
- HotChocolate.PersistedQueries.InMemory (13.9.4) - Query persistence
- HotChocolate.Types.Analyzers (13.9.4) - Code analyzers
Entity Framework Core
- Microsoft.EntityFrameworkCore (8.0.4) - ORM framework
- Npgsql.EntityFrameworkCore.PostgreSQL (8.0.4) - PostgreSQL provider
- EFCore.NamingConventions (8.0.3) - Snake case naming
HTTP & WebSocket Clients
- Refit (7.0.0) - Type-safe REST client
- Websocket.Client (5.1.1) - WebSocket client
- Microsoft.Extensions.Http.Polly (8.0.6) - Resilience and fault handling
- Polly.Core (8.4.0) - Resilience policies
SPA Integration
- Microsoft.AspNetCore.SpaServices.Extensions (8.0.5) - Serve React app
Development Commands
Running the Application
dotnet watch enables hot reload - changes to C# files will automatically rebuild and restart the application.Database Migrations
Development Workflow
Start the Development Server
Access GraphQL Playground
Navigate to
http://localhost:5000/graphql to access Banana Cake Pop, the GraphQL IDE provided by HotChocolate.Make Code Changes
Edit files in the
src/ directories. The dotnet watch command will detect changes and automatically rebuild and restart the application.GraphQL Schema
PriceSignal uses HotChocolate’s code-first approach to define the GraphQL schema.Schema Features
- Queries - Fetch cryptocurrency prices, user alerts, and settings
- Mutations - Create, update, delete alerts and user preferences
- Subscriptions - Real-time price updates via WebSocket
- Authorization - Firebase authentication integration
Adding New Queries
- Create a query class or add to existing query type
- Decorate with HotChocolate attributes
- Use dependency injection for services
- Return strongly-typed results
Adding New Mutations
- Create input and payload types
- Define mutation method
- Implement business logic
- Return success/error payload
Adding New Subscriptions
- Define subscription resolver
- Use
ITopicEventReceiverfor event streaming - Publish events from mutations or background services
Database Access
DbContext
TheApplicationDbContext is located in the Infrastructure project and defines:
- Entity configurations
- Database schema
- Relationships
Creating Entities
- Define entity in Domain project
- Configure in
ApplicationDbContext - Create migration
- Apply migration to database
Querying Data
Use Entity Framework Core with HotChocolate’s data integration:External Integrations
Binance Integration
Configuration inappsettings.json:
- Real-time price data via WebSocket
- Historical data via REST API
Alpaca Markets Integration
Configuration inappsettings.json:
- Cryptocurrency market data
- Price history and OHLCV data
Configuration
Application Settings
Theappsettings.json file contains:
- Logging - Configure log levels
- AllowedHosts - CORS configuration
- ConnectionStrings - Database connections
- External APIs - Binance, Alpaca configuration
User Secrets (Development)
Store sensitive configuration in user secrets:Environment Variables
For production deployments, use environment variables:Testing
Running Tests
Writing Tests
Create test projects for:- Unit tests for business logic
- Integration tests for API endpoints
- GraphQL query/mutation tests
Building for Production
Copy Frontend to wwwroot
The build process should copy files from
dist/ to src/PriceSignal/wwwroot/.Docker Support
The project includes Docker support (DockerDefaultTargetOS is set to Linux).Building Docker Image
Running with Docker Compose
Ensure your docker-compose.yml includes PostgreSQL service and proper environment variables for the connection string.
Common Tasks
Adding a New External API Client
- Define interface in Application layer
- Implement using Refit in Infrastructure
- Configure HTTP client with Polly policies
- Register in dependency injection
Adding a New Background Service
- Create class implementing
BackgroundService - Implement
ExecuteAsyncmethod - Register in
Program.cs
Implementing Real-time Updates
- Define subscription in GraphQL schema
- Create background service to monitor data source
- Publish events using
ITopicEventSender - Subscribe from frontend using GraphQL subscriptions
Troubleshooting
Port Already in Use
Change ports inProperties/launchSettings.json or use environment variables:
Database Connection Issues
Verify:- PostgreSQL is running
- Connection string is correct
- Database exists
- User has proper permissions
Migration Errors
If migrations fail:- Ensure PostgreSQL is running
- Check connection string
- Verify Infrastructure project builds successfully
- Try removing and recreating the migration
Next Steps
- Frontend Development - Learn about the React frontend
- Environment Setup - Initial setup guide