← Back to API and Web Development

GraphQL

GraphQL lets clients request exact API fields through a typed schema.

GraphQL is a query language and execution model for APIs. Instead of exposing many fixed endpoints that each return a predefined response shape, a GraphQL server exposes a typed schema and lets the client ask for exactly the fields it needs. That changes how APIs are consumed. The client describes the data shape, and the server resolves that request against its underlying systems.

A GraphQL schema defines object types, fields, relationships, and operations. The main operation types are queries for reads, mutations for writes, and sometimes subscriptions for real time updates. Each field in the schema is backed by resolver logic. A resolver might read from a database, call another service, or compute a value from parent data. This separation between schema and data sources is part of the appeal. Clients see one graph shaped interface even when the backend is assembled from many systems.

The usual benefit is efficiency for clients. A mobile screen might need a user's name, avatar, and three recent notifications. With a REST API, the client may have to call several endpoints or accept a response that contains more data than it needs. With GraphQL, the client can request exactly those fields in one round trip. That reduces overfetching and underfetching, which is why GraphQL became popular for complex front ends with many distinct views.

The tradeoffs appear in server design. Because clients choose the shape, caching is harder than with fixed HTTP responses. Field level resolvers can also create the N+1 query problem, where fetching a list of objects triggers one database query for the list and then one more query per item. Good GraphQL servers use batching and caching layers, such as data loaders, to combine those lookups efficiently.

Security and governance are also different. A single flexible endpoint means authorisation has to be enforced at the field and object level, not just by URL path. Query depth and breadth must be controlled so a client cannot ask for an extremely expensive traversal or huge result set. Schema evolution is gentler than versioned REST because new fields can be added without breaking existing clients, but old fields still need deprecation policy and eventual removal discipline.

GraphQL works best when the application has many clients or view specific data needs and when the team is willing to invest in schema design. It is especially useful as an aggregation layer over multiple backend services because it can present a consistent interface while hiding internal fragmentation.

It is less compelling when the domain is simple, the data access patterns are uniform, or the team is not prepared to manage query cost, field level permissions, and resolver performance. GraphQL is not automatically simpler than REST. It trades endpoint proliferation for schema complexity. When used deliberately, that is a good trade. When adopted only for fashion, it can become an elegant layer on top of slow queries and unclear ownership.