REST vs GraphQL

GraphQL is a new API language and protocol that addresses REST's shortcomings. We compare and contrast the two and provide guidelines on when each should be used.

Problems with REST

REST (Representational State Transfer) is the standard way of building web APIs, but it has its drawbacks. REST maps resources and methods to URLs, and uses HTTP as a transport layer. However, this can be inefficient and inflexible, particularly when working with large or complex data sets.

For example, consider a book library API. In REST, we might have endpoints for /books, /authors, and /publications, with methods for creating, reading, updating, and deleting data. If we want to list all of the books in the library, we would do a call to an URL like /books/. The server might return all information about the books, but since we're only interested in book titles, it would be inefficient to pull up all the book info from the database and send it over the wire. Some might work around this by adding another endpoint, /book-titles/ that only returns the book titles. Others might use the request query string to list the fields wanted, like /books/?fields=name.

GraphQL as a solution

Enter GraphQL, a query language for APIs that allows clients to specify exactly the data they need. This makes it more flexible and efficient than REST. Instead of defining endpoints for each resource, GraphQL defines types and fields for the data available on the server. Clients can then query this data using a simple, predictable syntax.

With GraphQL, we can define the book, publication and author resources as data types, along with queries and mutations to allow clients to fetch and modify data. In this example, we define the Book, Author, and Publication types, along with queries and mutations to allow clients to fetch and modify data. Here is an example schema:

type Book {
  id: ID
  title: String
  author: Author
  publication: Publication
}

type Author {
  id: ID
  name: String
  books: [Book]
}

type Publication {
  id: ID
  year: Int
  publisher: String
  books: [Book]
}

type Query {
  books: [Book]
  authors: [Author]
  publications: [Publication]

  bookById(id: Int): Book
  authorById(id: Int): Author
  publicationById(id: Int): Publication
}

type Mutation {
  addBook(title: String, authorId: Int, publicationId: Int): Book
  addAuthor(name: String): Author
  addPublication(year: Int, publisher: String): Publication
}

To query this data, a client would issue a POST request with a GraphQL query in the request body. For example, to fetch a list of books with the book title and author name included, the client might send the following query:

query {
  books {
    title
    author {
      name
    }
  }
}

The server would then respond with a JSON object containing the requested data:

{
  "data": {
    "books": [
      {
        "title": "The Great Gatsby",
        "author": {
          "name": "F. Scott Fitzgerald"
        }
      }
    }
  }
}

Drawbacks of GraphQL

One of the main problems with GraphQL is that it can be difficult to understand and use, especially for developers who are not familiar with its syntax and principles. This can lead to longer development times and potentially higher costs for organizations that use GraphQL. Additionally, because GraphQL is a relatively new technology, it may not have the same level of support and community resources as REST.

Another potential issue with GraphQL is that it can be less efficient than REST in some cases. Because GraphQL allows the client to specify exactly which data it needs from the server, the server must do more work to assemble the response. This can lead to slower response times and increased load on the server.

Which is better?

Overall, the choice between REST and GraphQL depends on the specific needs of the application and the organization. If an organization has a large and complex API with many different endpoints and data types, GraphQL may be a good choice because it allows the client to query the server for exactly the data it needs. However, if the API is relatively simple and well-defined, REST may be a better option because it is more widely understood and supported by the development community. Additionally, REST may be a better choice if efficiency is a concern, because its API endpoints can be optimized for specific use cases.