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.
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 pagesVerified 9 Nov