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
+1-1
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ Why use GraphQL? It turns out that GraphQL is amazingly useful—it combines a n
25
25
26
26
-**Flexible, explicit queries:** GraphQL puts the consumer of the API in full control over what data they receive. Instead of a REST endpoint that returns all the properties you could possibly want (and often links to more), you only get the properties you ask for.
27
27
-**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
-
-**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.
28
+
-**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
29
-**Query consolidation:** A request for multiple data types can be combined into a single query that is executed in parallel on the server.
30
30
-**Static query analysis:** GraphQL schemas allow you to statically analyze the queries in your codebase and guarantee that you’ll never break them.
Copy file name to clipboardexpand all lines: text/README.md
+28-21
Original file line number
Diff line number
Diff line change
@@ -17,12 +17,17 @@ This book is for most programmers. If you write software that fetches data from
17
17
18
18
This book will be especially poignant to these groups of people:
19
19
20
-
- Backend devs who work on REST APIs and write a lot of similar data-fetching code, or who maintain view-specific endpoints.
21
-
- Frontend devs of medium- or large-sized apps who either: A) don’t use a caching library, and manually keep track of what data has already been fetched from the server, or B) use a cache, and write a lot of code to fetch data over REST and put it in the cache (we’re looking at you, Reduxers 👀😄).
20
+
- Backend devs who work on REST APIs and who:
21
+
- write a lot of similar code to fetch data and format it into JSON,
22
+
- maintain view- or device-specific endpoints, or
23
+
- have multiple APIs that use overlapping business data.
24
+
- Frontend devs who either:
25
+
- don’t use a caching library, and manually keep track of what data has already been fetched from the server, or
26
+
- use a cache, and write a lot of code to fetch data over REST and put it in the cache (we’re looking at you, Reduxers 👀😄).
22
27
23
28
# Background
24
29
25
-
We have a [Background](bg.md)section that provides concise introductions to various background topics. You’re welcome to either look through them now or individually as you go along—at the beginning of a section, you’ll find a list of topics it assumes knowledge of, like the [Anywhere: HTTP](5.md#anywhere-http) section, which has two listed:
30
+
We have a [Background](bg.md)chapter that provides concise introductions to various background topics. You’re welcome to either look through them now or individually as you go along—at the beginning of a section, you’ll find a list of topics it assumes knowledge of, like the [Anywhere: HTTP](5.md#anywhere-http) section, which has two listed:
*[localStorage vs. cookies](bg.md#localstorage-vs-cookies)
48
+
*[localStorage vs. cookies](bg.md#localstorage-vs-cookies)
49
+
*[Browser performance](bg.md#browser-performance)
43
50
44
51
Some, like *Git* and *Node*, are necessary for following along with the coding. Others, like *Tokens vs. sessions*, are nice to know, but not necessary.
45
52
@@ -65,22 +72,22 @@ Chapters [2](2.md) and [3](3.md) explain the language itself and its type system
65
72
66
73
# The code
67
74
68
-
We intersperse blocks of code throughout the text. When we add code to a file that we’ve shown previously, we often just display the additions and some context, with ellipses (…) in place of existing code. These additions will be clearest if you read the book with the code open in another window. Further, we believe people tend to learn better if they write things out themselves, so we encourage you to write out the code for each step, and get it working on your computer before moving on to the next step.
75
+
We intersperse blocks of code throughout the text. When we add code to a file that we’ve shown previously, we often just display the additions and some context, with ellipses (`...`) in place of existing code. These additions will be clearest if you read the book with the code open in another window. Further, we believe humans usually learn better if they write things out themselves, so we encourage you to write out the code for each step, and get it working on your computer before moving on to the next step.
69
76
70
-
We recommend the Chrome browser and the [VS Code](https://code.visualstudio.com/) editor.
77
+
We recommend using Chrome and [VS Code](https://code.visualstudio.com/).
71
78
72
-
If you’re reading this in epub or mobi format on your phone, turning sideways into landscape mode will help reduce code snippet wrapping.
79
+
Code snippets are better formatted and sized in the PDF version of the book. If you’re reading this in EPUB or MOBI format on your phone, turning sideways into landscape mode will help reduce code snippet wrapping.
73
80
74
81
## Git
75
82
76
-
In Chapters 6–11, you’ll learn through writing an app, step by step. Each chapter has its own repository. Each step has a branch in that repo, for example branch `0` is the starter template, branch `1` has the code you write in step 1, etc. The branches we link to in the text also have a version number, and have the format: `[step]_[version]`. When this version of the Guide was published, the version of the Chapter 6 code was `0.1.0`, so step 1 links to branch `1_0.1.0`.
83
+
In Chapters 6–11, you’ll learn through writing an app, step by step. Each chapter has its own repository. Each step has a branch in that repo, for example branch `0` is the starter template, branch `1` has the code you write in step 1, etc. The branches we link to in the text also have a version number, and have the format: `[step]_[version]`. When this version of the Guide was published, the version of the Chapter 6 code was `0.1.0`, so step 1 linked to branch `1_0.1.0`. The current version of the code is `0.2.0`, so step 1 links to `1_0.2.0`.
77
84
78
-
If you skip the beginning of Chapter 6 and go straight to the [Listing reviews](6.md#listing-reviews) section, it says to start with step 9 (`9_0.1.0`). So we can look at the app in that state with these terminal commands:
85
+
If you skip the beginning of Chapter 6 and go straight to the [Listing reviews](6.md#listing-reviews) section, it says to start with step 9 (`9_0.2.0`). So we can look at the app in that state with these terminal commands:
We can also see the solution to the current step by checking out the next step:
99
106
100
107
```sh
101
-
git checkout 10_0.1.0
108
+
git checkout 10_0.2.0
102
109
npm start
103
110
```
104
111
@@ -119,9 +126,9 @@ Which means `'` instead of `"` for string literals and no unnecessary semicolons
119
126
120
127
If you run into issues, we recommend posting to Stack Overflow with the relevant tag, for instance [`react-apollo`](https://stackoverflow.com/questions/ask?tags=react-apollo) for Chapter 6. If you have the Full edition, you can also ask the community in the #support Slack channel or email the technical support address we gave you.
121
128
122
-
If the issue is with our code, please search the repository’s issues to see if it’s an existing bug, and if it’s new, submit it! 🙏🙌
129
+
If the issue is with our code, please search the repository’s issues to see if it’s an existing bug, and if it’s new, submit it! 🙏🙌
Another important resource is the docs! Here they are for each library:
127
134
@@ -133,18 +140,18 @@ Another important resource is the docs! Here they are for each library:
133
140
134
141
# Version
135
142
136
-
Book version: [`r2`](https://github.com/GraphQLGuide/book/releases)
143
+
Book version: `r3` ([changelog](https://github.com/GraphQLGuide/book/releases))
137
144
138
-
Published June 27, 2018
145
+
Published April 10, 2019
139
146
140
-
As we write more of the book, we’ll send you new versions of it (using the email address on the GitHub account you connected when you purchased the book at[graphql.guide](https://graphql.guide)).
147
+
As we write more of the book, we’ll send you new versions of it (using the email address on the GitHub account you connected when you purchased the book from[graphql.guide](https://graphql.guide)).
Copy file name to clipboardexpand all lines: text/bg.md
+40-8
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,6 @@ Chapter contents:
17
17
*[localStorage vs. cookies](bg.md#localstorage-vs-cookies)
18
18
*[Browser performance](bg.md#browser-performance)
19
19
20
-
21
20
---
22
21
23
22
This chapter provides concise introductions to various background topics. You’re welcome to either read them all up front or individually as you go along—at the beginning of a section, you’ll find a list of topics it assumes knowledge of, like the [Anywhere: HTTP](5.md#anywhere-http) section, which has two listed:
HTTP is a format for sending messages over the Internet. It is used on top of two other message formats—IP (which has an *IP address* and routes the message to the right machine) and TCP (which has a port number and resends any messages that are lost in transit). An HTTP message adds a *method* (like `GET` or `POST`), a path (like `/graphql`), headers (like the `Bearer` header we use for [authentication](#authentication)), and a body (where GraphQL queries and responses go).
204
+
HTTP is a format for sending messages over the Internet. It is used on top of two other message formats—IP (which has an *IP address* and routes the message to the right machine) and TCP (which has a port number and resends any messages that are lost in transit). An HTTP message adds a *method* (like `GET` or `POST`), a path (like `/graphql`), headers (like the `Bearer` header we use for [authentication](#authentication)), and a body (where GraphQL queries and responses go).
205
+
206
+
When we enter a URL like `http://graphql.guide/` into our browser, it goes through these steps:
207
+
208
+
1. Browser asks DNS server what the IP address of `graphql.guide` is.
209
+
2. DNS server responds with `104.27.191.39`.
210
+
211
+
We can see for ourselves what the DNS server says using the `nslookup` command:
212
+
213
+
```sh
214
+
$ nslookup graphql.guide
215
+
Server: 8.8.4.4
216
+
Address: 8.8.4.4#53
217
+
218
+
Non-authoritative answer:
219
+
Name: graphql.guide
220
+
Address: 104.27.191.39
221
+
```
222
+
223
+
3. Browser sends out a message to the Internet that looks like this:
224
+
225
+
```
226
+
IP to 104.27.191.39
227
+
TCP to port 80
228
+
HTTP GET /
229
+
```
230
+
231
+
4. Internet routers look at the IP part, see it is addressed to `104.27.191.39`, and pass it off to a router that is closer to `104.27.191.39`.
232
+
233
+
5. The message arrives at `104.27.191.39` (the IP address of the Guide server), which opens the message, sees that it's trying to connect to port 80, and passes the message to whatever server program (in this case a Node.js process) is listening at the port.
234
+
235
+
6. The server process sees that the client wants to GET /, the root path, and sends back an `index.html` to the client.
236
+
237
+
> This process is a little simplified—it actually takes a separate round-trip message to set up the TCP connection, and for `graphql.guide`, the client is actually redirected to HTTPS at the beginning, which uses port 443 and sets up an SSL connection before sending HTTP GET /.
206
238
207
239
# SPA
208
240
@@ -263,7 +295,7 @@ Then our server parses the JSON to figure out what happened. In this case, the `
263
295
264
296
# Continuous integration
265
297
266
-
While continuous integration (CI) technically means merging to master frequently, in modern web development it usually means the process of tests being run automatically on each commit. It's often done with a service like [CircleCI](https://circleci.com/) that monitors our commits on Github, runs the tests on their servers, and provides a webpage for each commit where we can view the test output. We can also set it up to do something after the test, such as:
298
+
While continuous integration (CI) technically means merging to master frequently, in modern web development it usually means the process of tests being run automatically on each commit. It's often done with a service like [CircleCI](https://circleci.com/) that monitors our commits on GitHub, runs the tests, and provides a webpage for each commit where we can view the test output. We can also set it up to do something after the test, such as:
267
299
268
300
- Mark a pull request as passing or failing the tests.
269
301
- Mark that commit as passing or failing by adding a red X or green checkmark next to the commit in the repository's history.
@@ -311,18 +343,18 @@ While the XSS issue is a serious concern, a common mitigation is setting short e
311
343
312
344
# Browser performance
313
345
314
-
Users notice when sites are slow, and they don't like it 😄. So if we want our users to feel good using our site, we want things in the browser to happen at a certain speed.
346
+
Users notice when sites are slow, and they don't like it 😄. So if we want our users to feel good using our site, we want different things in the browser to happen at certain speeds.
315
347
316
348
First let's go over how the browser works. Because JavaScript is single-threaded, it can only run on a single CPU core. We can have particular pieces of JS run in [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers), which can run on different cores, but most of our JS runs on one core, in the browser's **main thread**. The browser also needs to do most of its page **rendering** (parsing HTML and CSS, laying out elements, painting pixels into images, etc) in the main thread.
317
349
318
350
> **Composition**, in which the pixel images are positioned, happens on the GPU.
319
351
320
352
A CPU core has a limited speed—it can only do a certain amount of work each millisecond. And because both JS and rendering happen on the same core, every millisecond our JS takes up is another millisecond the browser rendering has to wait before it can run. And the user won't see the page update until the browser has a chance to render.
321
353
322
-
Now that we know what's going on, let's think about different situations the user is in, and how fast our site should be in each:
354
+
Now that we know what's going on, let's think about different situations the user is in and how fast our site should be in each:
323
355
324
356
-**Page load:** The faster the better, but good targets are under 5 seconds *time to interactive* (the page is interactive when content has been displayed and the page is interactable—it can be scrolled, things can be clicked on, etc.) for the first visit and under 2 seconds for subsequent visits.
325
-
-**Response:** When humans take an action like clicking a button, and the page changes within 100 milliseconds, they generally perceive the response as immediate. If the response takes over 100ms, humans perceive a delay. If our event handler runs code that takes 100ms on slow devices, then we want to break the code into two pieces: the minimum amount that will trigger the desired UI change, and the rest. And we schedule the rest to be done later:
357
+
-**Response:** When humans take an action like clicking a button, and the page changes within 100 milliseconds, they generally perceive the response as immediate. If the response takes over 100ms, humans perceive a delay. If our click event handler runs code that takes 100ms on slow devices, then we want to break the code into two pieces: the minimum amount that will trigger the desired UI change, and the rest. And we schedule the rest to be done later:
326
358
327
359
```js
328
360
button.onclick= () => {
@@ -342,6 +374,6 @@ class Foo extends Component {
342
374
}
343
375
```
344
376
345
-
[requestIdleCallback()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback) runs the given function when the browser is idle, after it has finished rendering the changes triggered by `updateUI()`.
377
+
[requestIdleCallback()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback) runs the given function when the browser is idle, after it has finished rendering the changes triggered by `updateUI()`/`this.setState()`.
346
378
347
-
-**Animation:** Humans perceive a motion as smooth at 60 fps—when 60 frames are rendered per second. If we take 1000 milliseconds and divide by 60, we get 16. So while something is moving on the page, we want the browser to be able to render every 16ms. The browser needs 6ms to paint, which gives us 10ms left to run JS in. "Something moving" includes visual animations like entrances/exits and loading indicators, scrolling, and dragging.
379
+
-**Animation:** Humans perceive a motion as smooth at 60 fps—when 60 frames are rendered per second. If we take 1000 milliseconds and divide by 60, we get 16. So while something is moving on the page, we want the browser to be able to render every 16ms. The browser needs 6ms to paint, which gives us 10ms left to run JS in. "Something moving" includes visual animations like entrances/exits and loading indicators, scrolling, and dragging.
0 commit comments