You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/1.md
+12-8
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# Chapter 1: Understanding GraphQL Through REST
2
2
3
+
> Chapter 1 is a basic introduction to GraphQL through the eyes of the technology it’s replacing, REST. If you’re already sold on GraphQL and familiar with the basics, feel free to skip ahead to [Chapter 2: Query Language](2.md) for theory or [Chapter 5: Client Dev](5.md) and [Chapter 11: Server Dev](11.md) for coding.
4
+
3
5
Chapter contents:
4
6
5
7
*[Introduction](1.md#introduction)
@@ -27,12 +29,12 @@ Why use GraphQL? It turns out that GraphQL is amazingly useful—it combines a n
27
29
-**Type-safe, self-documenting API:** GraphQL APIs are type-safe and self-documenting: the schema you define is exposed as interactive documentation for private or public consumption.
28
30
-**No more API endpoint sprawl:** Backend engineers also love GraphQL. Once you write the code for accessing a data type, you won’t have to re-implement it. You don’t have to make a new endpoint for each view—you can leave that work up to the client and the GraphQL execution model, which is implemented by your GraphQL server library. And you don’t have to make a new API for each new app—you can have a single GraphQL API that covers all your business data.
29
31
-**Query consolidation:** A request for multiple data types can be combined into a single query that is executed in parallel on the server.
30
-
-**Static query analysis:** GraphQL schemas allow you to statically analyze the queries in your codebase and guarantee that you’ll never break them.
32
+
-**Static query analysis:** GraphQL schemas allow you to statically analyze the queries in your codebase, and they guarantee that you’ll never break them.
31
33
32
34
All together you end up with an API that is a delight to use and allows you to write expressive queries with understandable results:
33
35
34
36

35
-
*A GraphQL query (left) and the results from the server (right) inside the [GraphiQL web interface](https://ch1.graphql.guide/graphql).*
37
+
*A GraphQL query (left) and the results from the server (right) inside the GraphiQL (with an “i” and pronounced “graphical”) web interface, an easy-to-use IDE for developing GraphQL queries.*
36
38
37
39
By reading this book, you’ll learn why you should use GraphQL in production and how to do it, and some best practices and common pitfalls. To start, we’ll dip our toes in the water: we’ll see the power of GraphQL through examples and answer the most obvious questions. Then we’ll dive deep into the intricacies of how this system was designed.
38
40
@@ -52,8 +54,12 @@ We recommend following along with the code, which can be found [on GitHub](https
We’ll start our process of understanding GraphQL by building a simple REST API using Node.js and the popular Express web framework. We’ll be retrieving data from a MongoDB database and using Mongoose as our object-relational mapping (ORM) to simplify querying the data we have stored.
56
60
61
+
> Learn about MongoDB, the Node driver, and Mongoose in [Background > MongoDB](bg.md#mongodb).
62
+
57
63
In this application, our server will listen for requests to the `/users/:id` URL. We use the ID passed as a parameter in the URL (as specified by the `:id`) to query a user record from the database and return it as a JSON string. If we encounter any errors, we return a 500 error, and if we can’t find the user, we return a 404—all standard REST practices.
The Mongoose data models are stored in a separate file:
@@ -225,7 +229,7 @@ query {
225
229
226
230
In addition to having a custom language for specifying the schema, GraphQL also has a language for specifying queries. This is more verbose than a REST API: with REST, the query is embedded in the URL itself; in GraphQL, we specify the endpoint that we’re calling (`user`) along with the argument (`id` with a value of `"123"`), and we also list every `User` field we want the server to return. This extra syntax is what makes GraphQL so flexible and explicit: it gives us an exact list of the data we are attempting to fetch.
227
231
228
-
We have a couple options if we want to run this query and get the data back from the server. To start, let’s use the command line to show how a typical query might be executed:
232
+
We have a couple of options if we want to run this query and get the data back from the server. To start, let’s use the command line to show how a typical query might be executed:
229
233
230
234
```sh
231
235
$ curl -X POST -H "Content-Type:application/json" \
No, in fact! Every time we query a GraphQL endpoint, we get a valid JSON response. In this case `"user"` is `null`, as its value wasn’t able to be determined. This becomes very useful when we handle permissions and errors, which we’ll get into [later](#security-&-error-handling).
248
252
249
-
When we run our server (`node graphql-server.js`) and visit [localhost:3000/graphql](http://localhost:3000/graphql) in the browser, we see the fantastic, easy-to-use web interface called GraphiQL (with an “i” and pronounced “graphical”):
253
+
When we run our server (`node graphql-server.js`) and visit [localhost:3000/graphql](http://localhost:3000/graphql) in the browser, we see GraphiQL::
250
254
251
255

252
256
*The results returned from a query inside the GraphiQL web interface.*
253
257
254
258
> You can also try out a hosted version here: [ch1.graphql.guide/graphql](https://ch1.graphql.guide/graphql)
255
259
256
-
Seeing or using GraphiQL for the first time is often the moment that software engineers become GraphQL converts. It offers an an intuitive interface to read the documentation for and query a GraphQL schema. We can write a query, see the results returned from the server, and explore the documentation on the right that’s been automatically generated from the schema. This is something that REST can’t replicate without a ton of extra work or an additional framework. GraphQL is only just starting to pay off, though—we’ll see that as the REST API becomes more and more complex, the complexity of the equivalent GraphQL API remains the same.
260
+
Seeing or using GraphiQL for the first time is often the moment that software engineers become GraphQL converts. It offers an an intuitive interface to read the documentation for and query a GraphQL schema. We can write a query, see the results returned from the server, and explore the documentation on the right that’s been automatically generated from the schema. This is something that REST can’t replicate without a ton of extra work or an additional framework. GraphQL is only just starting to pay off, though—as the REST API becomes more and more complex, the complexity of the equivalent GraphQL API remains the same.
257
261
258
262
# Querying a set of data
259
263
@@ -805,6 +809,6 @@ In a modern application, the consumer needs to be in control of what data they c
805
809
806
810
While GraphQL has a number of new concepts to learn (GraphQL schemas, the query language, etc.), these features are designed to help us write our applications correctly from the start—whereas the prospect of building a comparable REST API can be absolutely overwhelming in its complexity.
807
811
808
-
GraphQL truly is the most developer-friendly way of building an API. It puts the consumer in full control of the data requested, and as such we avoid querying data that we don’t need. As an added bonus, there is clear, automatically-generated documentation we can browse to understand any new GraphQL API.
812
+
GraphQL truly is the most developer-friendly way of building an API. It puts the consumer in full control of the data requested, and we can therefore avoid querying data that we don’t need. As an added bonus, there is clear, automatically-generated documentation we can browse to understand any new GraphQL API.
809
813
810
814
The developers of GraphQL learned from REST’s challenges and mistakes over the years, and have turned the best parts into a streamlined interface that will surely be the standard for API design for many years to come. The rest of this book will dive deep into the benefits of GraphQL, how to implement it efficiently, and how to build the best applications using this technology.
0 commit comments