What is GraphQL?

GraphQL is a query language and runtime for APIs created by Facebook. Rather than fixed endpoints returning predefined data structures, GraphQL allows clients to specify exactly what data they need.

Clients send GraphQL queries describing their data requirements. Servers respond with precisely that data - nothing more, nothing less. This efficiency is particularly valuable for mobile applications with limited bandwidth and battery life.

REST vs GraphQL

Fixed Responses: REST endpoints return fixed data structures. A /users/123 endpoint returns all user data. If clients only need the user's name, they still receive all user fields.

Flexible Queries: GraphQL clients specify exactly what they want. A GraphQL query might request only the user's name and email, even if the user object has additional fields.

Over-fetching: REST clients often receive unnecessary data (over-fetching). GraphQL clients request only what they need.

Under-fetching: REST often requires multiple requests. A REST client needing user data plus their posts makes two requests. A GraphQL query can fetch both in one request.

GraphQL Query Structure

GraphQL queries describe the shape of requested data:

query {
user(id: 123) {
name
email
posts {
title
body
}
}
}

This query requests the user with ID 123, their name and email, and titles and bodies of their posts. The response structure mirrors the query structure.

GraphQL Schema

GraphQL uses a schema defining available types and operations. The schema describes what data is available and what queries are valid.

type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
}
type Post {
id: ID!
title: String!
body: String!
}

Operations

Query: Fetches data read-only. Equivalent to GET in REST.

Mutation: Modifies data. Equivalent to POST, PUT, DELETE in REST.

Subscription: Listens for real-time updates via WebSocket.

Mutations

GraphQL mutations specify changes to data:

mutation {
createUser(name: "John", email: "john@example.com") {
id
name
email
}
}

Subscriptions

GraphQL subscriptions push real-time data to clients via WebSocket:

subscription {
userCreated {
id
name
}
}

GraphQL Advantages

Efficiency: Clients request only needed data, reducing bandwidth usage.

Single Request: Complex data requirements can be fulfilled in a single request.

Strong Typing: GraphQL schema provides strong typing, enabling better tooling.

Introspection: Clients can query the schema, discovering available operations.

Real-time: Subscriptions enable real-time data pushing.

GraphQL Disadvantages

Complexity: GraphQL introduces more concepts than REST. Queries are more complex.

Caching: HTTP caching is more straightforward with REST. GraphQL caching is more complex.

Learning Curve: Developers must learn GraphQL concepts.

Monitoring: Monitoring GraphQL APIs is more complex because queries vary.

GraphQL Implementations

Apollo: Popular GraphQL client and server library. Apollo Server is widely used.

Relay: Facebook's GraphQL client framework.

Hasura: Instantly generates GraphQL APIs from databases.

Use Cases

GraphQL excels for:

  • Mobile Applications: Reducing bandwidth is crucial on mobile.
  • Complex Data Requirements: Fetching related data in single requests.
  • Rapidly Evolving APIs: GraphQL enables adding fields without breaking clients.
  • Real-time Applications: Subscriptions enable real-time updates.

REST vs GraphQL Trade-offs

REST's simplicity and HTTP caching benefits remain valuable. GraphQL's efficiency benefits are most pronounced for mobile applications.

Many organisations use both - REST for simple APIs, GraphQL for complex requirements.

The Future of GraphQL

GraphQL adoption continues growing. Federation enables combining multiple GraphQL servers. GraphQL databases like Hasura simplify backend requirements.

GraphQL is not replacing REST; instead, it is another tool for API development. Choosing between them depends on specific requirements.