GraphQL Server Architecture
A production GraphQL server is usually more than a schema and a few resolver functions. A clean architecture separates concerns between transport, GraphQL execution, application services, data access, and infrastructure.
A useful structure is:
HTTP / WebSocket Transport
↓
GraphQL Server
↓
Schema + Validation
↓
Resolvers
↓
Application Services
↓
Repositories / Data Sources
↓
Database / External APIs
For a Node.js application, a possible project structure is:
src/
graphql/
schema/
resolvers/
services/
repositories/
models/
middleware/
context/
config/
server.js
The exact structure is not mandatory. The important principle is separation of responsibilities.
GraphQL layer
Defines the API contract and coordinates execution.
Service layer
Contains application and business logic.
Repository/data-access layer
Handles persistence and external data access.
Infrastructure layer
Handles database connections, logging, configuration, queues, and other operational concerns.
Keeping these boundaries clear prevents resolver files from becoming large collections of database queries and business rules.
Schema + resolversBusiness/application logicData accessDatabase, cache, logging, configuration- A production GraphQL server benefits from layered architecture.
- Resolvers should coordinate rather than contain all business logic.
- Services provide reusable application behavior.
- Repositories can isolate persistence details.
Where should complex reusable business logic generally live in a layered GraphQL application?