1 What Is GraphQL
1 What Is GraphQL
A brief introduction to GraphQL along with features, bene ts and how it differs from the commonly used REST
architecture.
• GraphQL: A History
• How is GraphQL Different?
• The Sandwich Comparison
• Overfetching
• Speci cation…NOT Implementation
• Queries & Mutations
• Relational Queries
GraphQL: A History #
GraphQL is an open source query language created by Facebook. Before
GraphQL went open source in 2015, Facebook had used it internally for their
mobile applications since 2012 as an alternative to the common REST
architecture. As a result, network usage was reduced dramatically for
Facebook’s mobile applications because GraphQL made it more efficient with
data transfers.
Let’s say you want to fetch data from the Rest API and you say, “Hey REST API,
give me the titles of the available courses on Educative”. How this works is
that you have a specific endpoint or URL (in our case, Educative) that you are
hitting and that URL determines what data comes back. With the REST API,
you fetch a URL and that URL is returning typically something like JSON or a
Javascript object full of data. This results in either unwanted data that we
must filter through to fetch our required data or multiple trips (requests) to
cater to different queries. However, GraphQL is different.
How is GraphQL Different? #
Instead of an API where you hit a URL and accept whatever data comes back,
GraphQL allows you to ask for specific data, giving clients more control over
what information is sent.
Overfetching #
In the RESTful architecture, the backend defines what data is available for
each resource on each URL, while the frontend always has to request all the
information in a resource, even if only a part of it is needed.
In the worst case scenario, a client application has to read multiple resources
through multiple network requests. This is called overfetching. A query
language like GraphQL on the server-side and client-side lets the client decide
which data it needs by making a single request to the server.
Speci cation…NOT Implementation #
Queries are used for data fetching and mutations are used to modify server-
side data. In the example below, you will see that a query has the exact same
shape as the result. This essential GraphQL feature always provides you with
the expected results because it lets the server know exactly what the client is
asking for.
//GraphQL Query:
query{
course(id: "5"){
id
name
author
}
}
"data":{
"course":{
"id": "5",
"name": "Learn GraphQL with React",
"author": "Robin Wieruch"
}
}
A Sample Query
network layer, which is often HTTP, nor about the payload format, which is
usually JSON. In short, it isn’t opinionated about the application architecture
in general.
Relational Queries #
With GraphQL, we can make relational queries of multiple fields which
results in us getting all the data required in one trip (query), unlike the REST
architecture in which we would need to make multiple requests (one for each
field).
Key: Value:
// GraphQL Query:
author(id: "7") {
id
name
avatarUrl
articles(limit: 2) {
name
urlSlug
}
}
A Sample Query
We will now move on to a brief description of the GraphQL server and client.