graphql for rest dummies

If you come from the world of REST, things in GraphQL are going to look very strange at first. They only use POST requests. There is only one endpoint. What in the heavens??

GET requests

They’re gone now. Instead, there are queries.

query {
  getAuthor(id: 5) {
    name
    posts {
      title
    }
  }
}

The cool thing about GraphQL is that compared to REST, this would usually require requesting both the author, and the posts, and it would send along all the data about each. A GraphQL query allows us to request only what we need.

Where do the fields for a GraphQL query come from?

Schema

You describe the fields available by creating a GraphQL schema which is a collection of available types of data. The most common type being the object type.

You can almost see the 1:1 relationship to your ORM, which turns database rows into objects.

Resolvers

Like the many methods in a REST controller, a response for a GraphQL type is generated by functions called resolvers. Resolvers allow you handle queries, including any arguments that might be given.

POST, PUT, PATCH requests

These are called a mutation. A mutation, like a query, is a special type within a GraphQL schema. And mutations, like queries, have resolvers that can normalize or validate submitted data.

This type is very logically named, given its function is to mutate or change the state of the database.

Not so different

This is a basic overview that doesn’t cover some particulars of GraphQL like fragments or directives, but hopefully it’s clear that GraphQL isn’t completely unintelligble to people who have developed REST APIs. It has its own lexicon and abandons the semantics of individual HTTP methods, but the basics of fetching and updating data are still there.