Bookmarks

GraphQL

A query language for APIs and a runtime for executing those queries: the client names the fields it needs, and the server responds strictly according to a typed schema.

Also: GraphQL query language

checked 9 Nov

All terms

GraphQL solves two common problems of client-server APIs: the client receives extra data it never uses, or makes several requests to assemble a single page. A GraphQL query describes the whole result — fields, relations, arguments — and the server returns exactly that structure.

Schema and types

The API is described by a schema written in a type definition language: objects, lists, enums, and three root operations — Query (read), Mutation (change) and Subscription (event subscriptions). The server validates every query against the schema before executing it and rejects fields that do not exist.

graphqlSchema and query: the client lists the fields itself
type User {  id: ID!  name: String!  posts: [Post!]!} type Query {  user(id: ID!): User} query {  user(id: "4") {    name    posts { title }  }}

How execution works

The server parses the query into a tree of fields and walks it: for each field it calls a function, the resolver, which fetches data from a database or another service. The response arrives as a single JSON document with the same shape as the query. Usually one endpoint serves the whole API, most often over HTTP POST.

Where GraphQL comes from

GraphQL was created at Facebook in 2012 and open-sourced in 2015; since 2019 the specification has been maintained by the independent GraphQL Foundation. The specification describes the syntax, the type system and the execution rules, and is not tied to any programming language: implementations exist for Python, JavaScript, Go and others.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • RESTREST

    In REST, the shape of a response is set by the server and fixed for each resource; in GraphQL the client lists the fields it needs and gets everything in one request.

In depth in the references

where the topic is covered in detail