ENES

GraphQL

An API language where the app asks for exactly the fields it needs, in one request.

An app sends one query asking for a user's name and their last two orders to a single endpoint, which gathers them from three sources and returns exactly that shape

GraphQL is a way of building APIs in which the client writes a query describing the exact data it wants, and the server answers with JSON in that same shape. It was created at Facebook in 2012 to feed its mobile apps, released publicly in 2015, and is now maintained by the GraphQL Foundation, part of the Linux Foundation.

Compare it with a REST API. To show a profile screen, a REST app might call /users/7, then /users/7/orders, then /orders/91/items, and receive many fields it never shows. With GraphQL it sends one request to a single endpoint, usually /graphql, saying "user 7: name, and the last two orders with their totals", and gets just that. This avoids over-fetching (too much data) and under-fetching (too many round trips), which matters on slow mobile connections.

Behind the endpoint sits a schema: a typed description of every object and field the API offers, which tools use to autocomplete and validate queries before they run. Each field is filled in by a small function called a resolver, which can read from a database, a REST service or anything else. The flexibility has costs: caching is harder than with plain URLs, and a careless query can ask for so much nested data that it overloads the server, so production APIs limit query depth and cost.

Keep in mind

Go deeper

Related terms

More in Software