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.

Application Layers
GraphQL layer: Schema + resolvers
Defines and executes the API contract.
Service layer: Business/application logic
Contains reusable domain operations.
Repository layer: Data access
Encapsulates persistence and external data retrieval.
Infrastructure: Database, cache, logging, configuration
Provides operational dependencies used by the application.
Node.js GraphQL Application Architecture
Key Takeaways
  • 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.
Knowledge Check

Where should complex reusable business logic generally live in a layered GraphQL application?