Complete Intro Graphql
What Is Graphql

What is GraphQL

GraphQL is a language and a runtime that are designed for the purpose of implementing and consuming data APIs for web and mobile applications and any other client applications. For the clients, GraphQL is a language they can use to declaratively request their exact data needs. For the servers, GraphQL is a runtime for fulfilling these exact clients' data needs with predictable results.

f101
Figure 1. GraphQL is a language and a runtime

GraphQL provides a structure for servers to describe the data exposed in their APIs. With this structure and the query language it makes available to its clients, GraphQL makes the task of evolving APIs a lot easier. GraphQL also enables powerful developer tools among other advantages that we will discuss in the next section.

Although the first talk about GraphQL was at a React.js conference, GraphQL is not specific to React. It can be used by any frontend framework or library and on any platform. You can use GraphQL in frontend applications with a client like Apollo or Relay or by manually making calls to a GraphQL server. You do not need React, Apollo, or Relay to use GraphQL in your applications.

Facebook designed GraphQL to solve the problems they were facing in their mobile application and to better serve resource-constrained devices over slow networks. At the time, Facebook had a WEBKIT-based mobile application that was not working very well and a decision was made to do a full native re-write. GraphQL was born to optimize the data communication for that re-write.

GraphQL today powers many of Facebook applications including their main web and mobile applications. GraphQL is also used in many other big applications like GitHub and Yelp.

The Big Picture

An API, in general, is an interface that enables the communication between multiple components in an application. For example, an API can enable the communication that needs to happen between a web client and a database server. The client needs to tell the server what data it needs and the server needs to fulfill this client’s requirement with objects representing the data they asked for.

f102
Figure 2. The big picture of data APIs

There are different types of APIs and every big application needs them. When talking about GraphQL, we are specifically talking about the API type that is used to read and modify data, which is usually referred to as a "Data API".

GraphQL is one option out of many that can be used to provide applications with programmable interfaces to read and modify the data they need from data services. Other options include REST, SOAP, XML, and even SQL itself.

SQL (the Standard Query Language) might be directly compared to GraphQL because "QL" is in both names, after all. Both SQL and GraphQL provide a language to query data schemas. They can both be used to read and modify data.

For example, assuming that we have a table of data about a company’s employees, the following can be an example SQL statement to read data about employees in one department:

SQL statement for querying
SELECT id, name, email, birth_date, hire_date
FROM employees
WHERE department = 'ENGINEERING'

Here is another example SQL statement that can be used to insert data for a new employee:

SQL statement for mutating
INSERT INTO employees (name, email, birth_date, hire_date) +
VALUES ('John Doe', 'john@doe.name', '01/01/1990', '01/01/2020')

You can use SQL to communicate data operations like we did above. The relational databases that these SQL statements are sent to usually support different formats for their responses. Each SQL operation type will have a different response. A SELECT operation might return a single row or multiple rows. An INSERT operation might return just a confirmation, the inserted rows, or an error response.

While possible, SQL would be a terrible language to use for communicating data requirements by mobile and web applications because of many reasons. For example, there are very few customization options for SQL requests and responses and it would be really hard to inject custom logic that can be tied into authentication and authorization.

While SQL is directly supported by most relational databases, GraphQL is its own thing. GraphQL needs a runtime service of its own. You cannot just start querying databases using the GraphQL query language (at least not yet). You will need to use a service layer that supports GraphQL or implement one yourself. This book will show you how to do both.

JSON is another language that can be used to communicate data. Here is a JSON text that represents the data for an employee:

JSON object representing data
{
  "data": {
    "employees": [
      {
        id: 42,
        name: "John Doe",
        email: john@doe.name,
        birthDate: "01/01/1990",
        hireDate: "01/01/2020"
      }
    ]
  }
}

JSON is a popular language to communicate data from API servers to client applications. Most of the modern data API servers use JSON to fulfill the data requirements of a client application. GraphQL servers are no exception; JSON is the popular choice to fulfill the requirements of GraphQL data requests.

JSON can also be used by client applications to communicate their data requirements to API servers. For example, here is a possible JSON object that can be used to communicate the data requirement for the previous employee object response:

JOSN example for querying
{
  "select": {
    "fields": ["name", "email", "birthDate", "hireDate"],
    "from": "employees",
    "where": {
      "id": {
       "equals": 42
      }
    }
  }
}

GraphQL for client applications is another language they can use to express their data requirements. The following is how the same previous data requirement can be expressed with a GraphQL query:

GraphQL example for querying
{
  employees(id: 42) {
    name
    email
    birthDate
    hireDate
  }
}

The GraphQL query above represents the same data need as the JSON object, but as you can see it has a different and shorter syntax. A GraphQL server can be made to understand this syntax and translate it into what the actual data storage engine can understand (for example, it would translate it into SQL statements for a relational database). Then, the GraphQL server can take what the storage engine responds with and translate it into something like JSON or XML and send it back to the client application.

f103
Figure 3. The GraphQL language can be used to form generic data requests

This is nice because no matter what storage engine (or multiple storage engines) you have to deal with, with GraphQL you make API servers and client applications both work with a universal language for requests and a universal language for responses.

In a nutshell, GraphQL is all about optimizing data communication between a client and a server. This includes the client asking for the needed data, communicating that need to the server, the server preparing a fulfillment for that need, and communicating that fulfillment back to the client. GraphQL allows clients to ask for the exact data they need and make it easier for servers to aggregate data from multiple data storage resources.

At the core of GraphQL, there is a strong type system that is used to describe the data and organize the APIs. This type system gives GraphQL many advantages on both the server and the client sides. Types ensure that the clients ask for only what is possible and provide clear and helpful errors. Clients can use types to minimize any manual parsing  of data elements. GraphQL type system allows for rich features like having an introspective API and being able to build powerful tools for both clients and servers. One of the popular GraphQL tools that relies on this concept is called GraphiQL, which is a feature-rich browser-based editor to explore and test GraphQL requests.

GraphQL is a specification

While Facebook engineers started working on GraphQL in 2012, it was 2015 when they released a public specifications document for it. This document is currently hosted at facebook.github.io/graphql and it is maintained by Facebook and a community of other companies and individuals on GitHub. GraphQL is still an evolving language, but the specifications document was a genius start for the project because it defined standard rules and practices that all implementers of GraphQL runtimes need to adhere to. There have been many implementations of GraphQL libraries in many different programming languages and all of them closely follow the specification document and update their implementations when that document is updated. If you work on a GraphQL project in Ruby and later switch to another project in Scala, the syntax will change but the rules and practices will remain the same.

Alongside the specification document, Facebook also released a reference implementation library for GraphQL runtimes in JavaScript. JavaScript is the most popular programming language and the one closest to mobile and web applications which are two of the popular channels where using GraphQL can make a big difference. The reference JavaScript implementation of GraphQL is hosted at github.com/graphql/graphql-js and you can add it to a Node project with npm install graphql.

I started learning GraphQL by reading the documentation and the specification document. The specification document is a bit technical, but you can still learn a lot from it by reading its important parts. This book will not cover each and everything in the specification document, so I recommend that you skim through it once you are done with the book.

The specification document starts by describing the syntax of the GraphQL language. Let’s talk about that first.

GraphQL is a language

While the Q (for query) is right there in the name, querying is associated with reading. However, GraphQL can be used for both reading and modifying data. When you need to read data with GraphQL you use queries and when you need to modify data you use mutations. Both queries and mutations are part of the GraphQL language.

This is just like how you use SELECT statements to read data with SQL and you use INSERT, UPDATE, and DELETE statements to modify it. The SQL language has certain rules that you must follow. For example, a SELECT statement requires a FROM clause and can optionally have a WHERE clause. Similarly, the GraphQL language has certain rules that you must follow as well. For example, a GraphQL query must have a name or be the only query in a request.

Unlike SQL, which is a language for databases, GraphQL is a language for APIs. GraphQL is not a database technology and it is completely database-agnostic.

Both the GraphQL and the SQL languages are smaller in scope and different from programming languages like JavaScript or Python. You cannot use the GraphQL language to create User Interfaces or perform complex computations. These query languages have more specific use cases and they often require the use of other programming languages to make them work.

Never the less, I would like you to first think of these query languages by comparing them to programming languages and even the languages that we speak, like English. This is a very limited-scope comparison, but I think in the case of GraphQL it will make you understand and appreciate a few things about it.

The evolution of programming languages in general is making them closer and closer to the human languages that we speak. Computers used to only understand imperative instructions and that is why we have been using imperative paradigms to program them. However, computers today are starting to understand declarative paradigms and you can program them to understand wishes. Declarative programming has many advantages (and disadvantages), but what makes it such a good idea is that we always prefer to reason about problems in declarative ways. Declarative thinking is easy for us.

We can use the English language to declaratively communicate data needs and fulfillments. For example, imagine that John is the client and Jane is the server. Here is an English data communication session:

John: Hey Jane, how long does it take the sun light to reach planet earth?

Jane: A bit over 8 minutes.

John: How about the light from the moon?

Jane: A bit under 2 seconds.

John can also easily ask both questions in one sentence and Jane can easily answer them both by adding more words to her answer.

When we communicate using the English language, we understand special expressions like "a bit over" and "a bit under". Jane also understood that the incomplete second question is related to the first one. Computers, on the other hand, are not very good (yet) at understanding things from the context. They need more structure.

GraphQL is just another declarative language that John and Jane can use to do that data communication session. It is not as good as the English language, but it is a structured language that computers can easily parse and use. For example, here’s a hypothetical single GraphQL query that can represent both of John’s questions to Jane:

John’s questions to Jane in GraphQL
{
  timeLightNeedsToTravel(toPlanet: "Earth") {
    fromTheSun: from(star: "Sun")
    fromTheMoon: from(moon: "Moon")
  }
}

The example GraphQL request above uses a few of the GraphQL language parts like fields (timeLightNeedsToTravel and from), parameters (toPlanet, star, and moon), and aliases (fromTheSun and fromTheMoon). These are like the verbs and nouns of the English language.

GraphQL is a service

If you teach a client application to speak the GraphQL language, it will be able to declaratively communicate any data requirements to a backend data service that also speaks GraphQL. To teach a data service to speak GraphQL, you need to implement a runtime layer and expose that layer to the clients who want to communicate with the service. Think of this layer on the server side as simply a translator of the GraphQL language, or a GraphQL-speaking agent who represents the data service. GraphQL is not a storage engine, so it cannot be a solution on its own. This is why  you cannot have a server that speaks just GraphQL and you need to implement a translating runtime instead.

This layer, which can be written in any programming language, defines a  generic graph-based strongly-typed schema that servers can use to publish the capabilities of the data service they represent. Client applications that speak GraphQL can then query that schema within its capabilities.

A GraphQL request can be a query (read request), a mutation (write request), or a subscription (real-time data monitoring request). For all cases, the request is a simple string that a GraphQL service can interpret, execute, and resolve with data in a specified format. The popular response format that is commonly used for mobile and web applications is JSON.

Once a request is received and parsed by the runtime, the next step is to prepare the data the request is asking for. GraphQL uses special functions for this task. They are named resolvers and they represent most of the smart logic behind GraphQL’s power and flexibility. A resolver function is where we give instructions for the runtime service about how and where to access the raw data. For example, a resolver function might issue a SQL statement to a relational database, read a file’s data directly from the operating system, or update some cached data in a document database. A resolver function is directly related to a field in a GraphQL request and it can represent a single primitive value, an object, or a list of values or objects.

The core concept in a GraphQL service is the typed nature of its schema. The schema represents a set of types that describes the set of possible data objects that can be read (or updated) through the service. GraphQL standardized another server-side language dedicated to creating schema services, which is called the Schema Definition Language (SDL). Here’s an example type in a schema written with SDL:

Schema Definition Language example
type Employee(id: Int!) {
  name: String!
  email: String!
}

As you can see, the SDL object above defines a custom Employee type. This type represents an employee object which has name and email fields, both of which are strings that cannot be empty. Continuing the SQL analogy, the GraphQL SDL is like the CREATE statements that we use to define tables, views, and other database schema elements. The GraphQL SDL enables implementing GraphQL schema services without relying on programming languages. You can take any SDL and input it to any implementation of GraphQL runtimes and it will just work.

Who is using GraphQL

Developers interest in GraphQL is very clear and GraphQL’s adoption is growing fast. Besides Facebook, GraphQL is now used in many other major web and mobile applications like GitHub, Yelp, Pinterest, Twitter, The New York Times, Coursera, and Shopify. Given that GraphQL is a very young technology, this is an impressive list.

Many other smaller companies are using GraphQL as well. You can see the big list of companies using GraphQL at graphql.org/users.

There are a few conferences dedicated to GraphQL, including GraphQL Summit and GraphQL Europe. They have been attracting new developers who are interested in learning about GraphQL.

Everywhere I go, I see people interested in learning about GraphQL. Everything I write about GraphQL gets immense attention and starts interesting discussions. I predict that GraphQL will see a lot more growth in the next few years and we will see it adopted by big enterprise businesses all the way down to young small startups. I am betting big on GraphQL because not only it solves some technical challenges about data APIs, but it also solves their more challenging "political" problems! Let me tell you more about that in this next section.