Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction to GraphQL  ›  Lesson

What is GraphQL

What is GraphQL 12 minGraphQL is a query language for APIs in which the client describes the shape of the data it wants and the server returns JSON in exactly that shape, guided by a typed schema.

What is GraphQL

A bookshop app needs a book's title, its author's name and its three newest review ratings, and nothing else. With GraphQL the app writes that wish down as a query, sends it to the server, and gets back JSON in exactly that shape. This whole course works on that one bookshop API.

The rule

Describe the shape you want; the server fills it in.

GraphQL is a query language for APIs plus a set of rules a server follows to answer it. It has three parts that always travel together:

  • A schema: the server's typed menu of everything a client may ask for.
  • Operations: a query to read, a mutation to change, a subscription to listen.
  • Resolvers: server functions that fetch the value for each field.

Here is part of the bookshop schema, written in GraphQL's schema language:

type Query {
  book(id: ID!): Book
}

type Book {
  id: ID!
  title: String!
  price: Float!
  author: Author!
}

type Author {
  id: ID!
  name: String!
}

A client asks for a book and picks its fields:

query {
  book(id: "b1") {
    title
    author { name }
  }
}

The answer mirrors the question:

{ "data": { "book": { "title": "The Salt Road", "author": { "name": "Mira Kapoor" } } } }

GraphQL began inside Facebook in 2012, was published as an open specification in 2015, and is now looked after by the GraphQL Foundation. It is a specification, not a product: many servers in many languages implement it. It also says nothing about which database you use. Your data can live in PostgreSQL, another service or a file; resolvers hide that.

The shortcut

To read any GraphQL exchange, lay the query over the response. Every field name in the query becomes a key in the JSON, at the same depth, under a top-level data key.

Predicting the responseStep 1 of 4
  1. Find the root field

    The query starts at book(id: "b1"), so the JSON has data.book.

A common slip is to think GraphQL is a database query language like SQL. It is not: it talks to an API, and the server decides how each field is fetched.

Drills

Answer fast — trust the rule.

quick drill

What does a GraphQL response contain?

quick drill

Which part of a GraphQL API lists the types and fields a client may ask for?

quick drill

A query selects book { title author { name } }. Which key is NOT in the response?

quick drill

Which statement about GraphQL is true?

Remember

  • Describe the shape you want; the server fills it in, under data.
  • Shortcut: lay the query over the response; keys match fields at the same depth.
  • Trap: GraphQL is an API language, not a database query language.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainGraphQL in your own words

A friend asks what GraphQL is. Using the bookshop query for a book's title and author name, explain the roles of the schema, the query and the resolvers, and describe the shape of the response.