GraphQL: flexible query language for APIs

GraphQL (open source July 2015) by Facebook: query language for APIs, type system, single endpoint, client specifies what it needs. Apollo Server/Client, Relay, Hasura. Alternative to REST for multi-source data aggregation.

Open SourceWeb GraphQLFacebookMetaApolloRelayAPIOpen SourceWeb

REST’s limit

Typical REST APIs have two problems for mobile/SPA clients:

  • Over-fetching — endpoint returns more data than needed
  • Under-fetching — an operation requires calls to N endpoints

Facebook internally (2012) develops GraphQL to feed the iOS/Android mobile app with heterogeneous data (profile, feed, notifications) in a single optimised call.

The release

GraphQL spec is released as open source by Facebook on 14 July 2015. Leads: Lee Byron, Dan Schafer, Nick Schrock. The first stable version arrives in 2016. MIT licence; spec governed by GraphQL Foundation (Linux Foundation) from 2018.

Concepts

  • Schema — strongly typed type system with scalar, object, interfaces, unions, enums
  • Query — read operation, client specifies data shape
  • Mutation — write operation
  • Subscription — real-time stream (typically over WebSocket)
  • Resolver — server functions that resolve each field
type User {
  id: ID!
  name: String!
  posts: [Post!]!
}

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

Advantages

  • Single endpoint /graphql for everything
  • Client-specified data — no over/under-fetching
  • Typed schema — introspection, tooling
  • Versionless — evolution via deprecation
  • Strong tooling — GraphiQL, Apollo Studio, Insomnia, Postman

Disadvantages

  • N+1 queries — naive resolvers devastating; requires DataLoader
  • Caching — more complex than REST/HTTP cache
  • Complexity attacks — deep queries expensive; needs depth limit, cost analysis
  • Over-engineering for simple APIs

Implementations

  • graphql-js — reference, Node.js
  • Apollo Server/Client — most widespread ecosystem
  • Relay — Meta client for React
  • Hasura — GraphQL auto-generated from Postgres
  • PostGraphile — Postgres → GraphQL
  • Strawberry (Python), graphql-ruby, sangria (Scala), graphql-java, HotChocolate (.NET)

Federation and Gateway

  • Apollo Federation (2019, v1) / (2021, v2) — sub-graph composition into super-graph
  • Schema stitching — earlier approach
  • GraphQL Mesh — multi-source gateway

Relative decline 2022-2026

After the 2016-2020 enthusiasm, many companies return to REST, tRPC, OpenAPI-first. Reasons:

  • Apollo becoming more commercial
  • Operational complexity
  • Harder HTTP caching
  • Netflix, Twitter deprecate internal use
  • tRPC offers end-to-end type safety for fullstack TypeScript stacks

GraphQL remains dominant where multi-source aggregation is needed (BFF, API composition, public dev portals).

In the Italian context

GraphQL is used in:

  • Magento/Shopify/Commercetools (headless) e-commerce
  • Media publishing (Mediaset, GEDI)
  • Italian mobile apps with GraphQL BFF
  • Internal admin tools for data composition

References: GraphQL open source (14 July 2015). Facebook/Meta. Lee Byron, Dan Schafer, Nick Schrock. GraphQL Foundation (2018). Apollo Server/Client ecosystem. Apollo Federation v2 (2021).

Need support? Under attack? Service Status
Need support? Under attack? Service Status