Skip to content

GraphQL - Architecture

GraphQL itself is a specification, not a specific software implementation. It defines a standard for API communication, focusing on how clients should ask for data and how servers should respond. Key architectural concepts include:

  • Schema Definition Language (SDL): A human-readable way to define the capabilities of your API, including data types, queries, mutations, and subscriptions.
  • Query Language: The syntax clients use to request specific data, mirroring the shape of the desired response.
  • Server-Side Execution: A GraphQL server parses incoming queries, validates them against the schema, fetches data from various sources (databases, microservices, third-party APIs), and structures the response according to the query.
  • Transport Agnostic: GraphQL typically uses HTTP (specifically POST requests), but the specification doesn’t mandate a specific network protocol. It can work over WebSockets (for subscriptions), TCP, or others.
  • Database Agnostic: GraphQL servers can fetch data from any kind of backend: SQL databases, NoSQL databases, REST APIs, other GraphQL APIs, or even static data.

A GraphQL server can be implemented in various ways, often falling into one of these architectural patterns:

This is a common pattern, especially for new projects or when building a GraphQL API directly on top of a primary data source. The GraphQL server receives a query, parses it, and its resolver functions directly interact with a database (e.g., PostgreSQL, MongoDB) to fetch or modify data.

Description: A client (web/mobile app) sends a GraphQL query over HTTP to the GraphQL server. The server’s resolvers execute database operations (e.g., SQL queries, ORM calls) to fulfill the request. The server then shapes the data retrieved from the database into the JSON format requested by the client’s query and sends the response back.

Use Case: Greenfield projects, applications where GraphQL is the primary API layer interacting directly with the database.

2. GraphQL Server as an Integration Layer (API Gateway)

Section titled “2. GraphQL Server as an Integration Layer (API Gateway)”

This approach is invaluable for organizations with existing infrastructure like microservices, legacy systems, or multiple third-party APIs. The GraphQL server acts as a unified interface or API gateway, hiding the complexity of the underlying systems from the client.

Description: The client sends a GraphQL query to the GraphQL server. Instead of talking directly to a database, the server’s resolvers make requests to other downstream services (e.g., REST APIs, gRPC services, other GraphQL APIs). It aggregates the responses from these services, transforms them as needed, and constructs the final GraphQL response for the client.

Use Case: Unifying data access across multiple microservices, providing a modern API layer over legacy systems, simplifying data fetching for frontend applications that need data from diverse sources.

Often, a combination of the above two patterns is used. A GraphQL server might have direct access to a primary database for some parts of the data graph while also integrating with other internal or external services for other parts.

Description: When the GraphQL server receives a query, it determines the source for each requested field. Some resolvers might query a connected database directly, while others might fetch data from external APIs or microservices. The server combines data from all relevant sources to build the final response.

Use Case: Complex applications with both dedicated databases and integrations with other services, evolving systems where new features use a direct database connection while older features rely on existing APIs.

GraphQL provides a flexible abstraction layer. The client interacts with a single, well-defined GraphQL endpoint and schema, regardless of how the data is actually stored or fetched on the backend. This decoupling is a major architectural benefit.