Skip to content

Commit f6f348a

Browse files
committed
Post ch 11 tech review
1 parent f3a7185 commit f6f348a

File tree

8 files changed

+5790
-107
lines changed

8 files changed

+5790
-107
lines changed

text/1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ module.exports = { User }
117117

118118
We connect to the database and implement a schema for the User that has two fields, `_id` (the default ID field for MongoDB) and `username`, and we turn that schema into a model that lets Mongoose know that users should be stored in the `users` collection (it takes the given model name `'User'` and lowercases and pluralizes).
119119

120-
We’ll need to have some data in our database to start, so we’ll insert a couple of simple documents with string `_id` and `username` fields, looking something like this (in [Robo 3T](https://robomongo.org/), a desktop graphical interface for running MongoDB queries against a database):
120+
We’ll need to have some data in our database to start, so we’ll insert a couple of simple documents with string `_id` and `username` fields, looking something like this (in [MongoDB Compass](https://www.mongodb.com/products/compass), a desktop graphical interface for running MongoDB queries against a database):
121121

122122
![users collection in MongoDB](/img/mongo-users.jpg)
123123
*The two records stored in the “users” collection of the MongoDB database.*

text/11.md

Lines changed: 5293 additions & 27 deletions
Large diffs are not rendered by default.

text/2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ We can think of the selection set as the left-hand side of a JSON document (with
9999

100100
[fragments](http://graphql.org/learn/queries/#fragments)
101101

102+
[inline fragments](http://graphql.org/learn/queries/#inline-fragments)
103+
102104
# Variables
103105

104106
[variables](http://graphql.org/learn/queries/#variables)

text/5.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ written these days follows the
2222
in which one computer is always providing a service (a **server**), and another
2323
computer is always requesting the service (a **client**, such as a web browser or mobile app). In a GraphQL client–server model, the client
2424
makes GraphQL requests, and the server provides the service of responding to
25-
those requests. We’ll code GraphQL clients in the next few chapters and GraphQL servers
25+
those requests. We’ll code GraphQL clients in the next few chapters and a GraphQL server
2626
in the [last](11.md).
2727

2828
First we’ll make simple HTTP requests, which we can do from any computer. Most
@@ -38,7 +38,7 @@ libraries are [`Apollo iOS`](https://www.apollographql.com/docs/ios/),
3838
Native.
3939

4040
For each type of client and the server, the app we’ll go through building is
41-
[graphql.guide](https://graphql.guide)—a web or mobile app for reading the
41+
[graphql.guide](https://graphql.guide/Preface)—a web or mobile app for reading the
4242
GraphQL Guide.
4343

4444
# Anywhere: HTTP
@@ -53,7 +53,7 @@ request.
5353
## cURL
5454

5555
When we’re on the command line, we can use
56-
[cURL](https://en.wikipedia.org/wiki/CURL) ("See URL", a tool for making network
56+
[cURL](https://en.wikipedia.org/wiki/CURL) (See URL, a tool for making network
5757
requests, including HTTP requests):
5858

5959
```sh
@@ -171,7 +171,7 @@ languages. Here is some common functionality that the libraries might provide:
171171
- [Caching](#caching)
172172
- [DevTools](#devtools)
173173

174-
The first is useful anywhere—whether it’s a script, service, or website that’s mostly static (only displays a small amount of dynamically fetched data). The second is useful when we’re working in a typed programming language. The last three are extremely helpful for building applications: whether we’re making a web app, a mobile app, or a desktop app, we usually need to fetch and display a number of different types of data from the server, and decide which to fetch and display based on user interactions. We also want to remember what we requested in the past, because oftentimes when we need to display it again, we don’t need to fetch it again. Doing all of this ourselves can get really complicated, but advanced client libraries like Apollo can take care of a lot of it for us.
174+
The first is useful anywhere—whether it’s a script, service, or website that’s mostly static (only displays a small amount of dynamically fetched data). The second is useful when we’re working in a typed programming language. The last three are extremely helpful for building applications: whether we’re making a web app, a mobile app, or a desktop app, we usually need to fetch and display a number of different types of data from the server, and decide which to fetch and display based on user interactions. We also want to remember what we requested in the past, because often when we need to display it again, we don’t need to fetch it again. Doing all of this ourselves can get really complicated, but advanced client libraries like Apollo can take care of a lot of it for us.
175175

176176
## Streamlined request function
177177

@@ -262,11 +262,11 @@ Our query returns:
262262

263263
And our library saves the response in a normalized cache. The key to each object
264264
in the cache is a string of the format `"[type]:[object id]"`, for instance
265-
"User:1" for a User object with an id of 1:
265+
`"User:1"` for a User object with an id of 1:
266266

267267
```js
268268
cache = {
269-
"User:1" {
269+
"User:1": {
270270
id: "1",
271271
firstName: "Loren",
272272
hasRead: [
@@ -275,12 +275,12 @@ cache = {
275275
]
276276
},
277277
"Section:5_1": {
278-
"id": "5_1",
279-
"title": "Anywhere: HTTP"
278+
id: "5_1",
279+
title: "Anywhere: HTTP"
280280
},
281281
"Section:5_2": {
282-
"id": "5_2",
283-
"title": "Client Libraries"
282+
id: "5_2",
283+
title: "Client Libraries"
284284
}
285285
}
286286
```
@@ -306,9 +306,9 @@ Which should return:
306306
}
307307
```
308308

309-
But we already know from our first query that "Client Libraries" is the title.
309+
But we already know from our first query that `"Client Libraries"` is the title.
310310
Our normalized caching library, instead of sending our second query to the
311-
server, can find the object with the "Section:5_2" key in the cache and
311+
server, can find the object with the `"Section:5_2"` key in the cache and
312312
immediately return it to us.
313313

314314
Sometimes we want to re-request data from the server, because it may have
@@ -317,7 +317,7 @@ us to re-request, but we usually want to immediately display the cached data
317317
while we wait for the response to arrive. The time it takes to get a value from
318318
the cache and display it on the screen could be as little as 20ms, whereas
319319
common response times from GraphQL servers are on the order of hundreds of
320-
milliseconds or seconds, depending on the user’s network connection [#latency] and
320+
milliseconds or seconds, depending on the user’s network connection [latency](bg.md#latency) and
321321
how long the server takes to retrieve their data. Humans
322322
[perceive delays](https://developers.google.com/web/fundamentals/performance/rail)
323323
above 100ms, so UX best practice is to show something on the screen within 100ms
@@ -327,20 +327,19 @@ displaying a spinner—or display the cached version of the data.
327327

328328
## DevTools
329329

330-
Some libraries have browser DevTools extensions for viewing information about an app’s GraphQL operations and the current state of the store. Apollo’s DevTools also has a built-in GraphiQL that uses the same network link as our app, so we don’t have to manually set authentication headers. We can even use it to query the store instead of the server!
330+
Some libraries have browser DevTools extensions for viewing information about an app’s GraphQL operations and the current state of the store. Apollo’s DevTools also has a built-in GraphiQL that uses the same network link as our app, so we don’t have to manually set authentication headers. We can even use it to query the store instead of the server by checking “Load from cache”:
331331

332332
![DevTools GraphiQL](img/devtools-graphiql.png)
333333

334-
Using the "Load from cache" feature of the built-in GraphiQL
334+
Under the Queries tab, we see the current “Watched queries” (queries attached to our components) and what variables they were called with:
335335

336336
![DevTools Query](img/devtools-query.png)
337337

338-
See the current "Watched Queries" (queries attached to our components) and what variables they were called with.
338+
Under the Mutations tab, we see a log of all past mutations and their variables:
339339

340340
![DevTools Mutation](img/devtools-ViewedMutation.png)
341341

342-
See a log of all past mutations and their variables.
342+
Under the Cache tab, we see the current state of the store/cache. Normalized data objects (any objects for which the `id` was requested) are listed on the left and appear in expandable `<details>` nodes on the right:
343343

344344
![DevTools Cache](img/devtools-cache.png)
345345

346-
See the current state of the store/cache. Normalized data objects (any objects for which the `id` was requested) are listed on the left and appear in expandable `<details>` nodes on the right.

text/6.md

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,12 @@ Chapter contents:
6060

6161
---
6262

63-
Background: [single-page application](bg.md#spa), [HTTP](bg.md#http), [Node](bg.md#node-&-npm-&-nvm), [git](bg.md#git), [JSON](bg.md#json), [JavaScript](bg#javascript)
63+
Background: [single-page application](bg.md#spa), [HTTP](bg.md#http), [Node](bg.md#node-&-npm-&-nvm), [git](bg.md#git), [JSON](bg.md#json), [JavaScript](bg#javascript), [React](bg.md#react)
6464

6565
In this chapter, we’ll learn to use the [`react-apollo`](https://www.apollographql.com/docs/react/) library through building the Guide web app—the code behind the [https://graphql.guide](https://graphql.guide/Preface) site, where we can sign in, read the book, and write reviews. *[Beta note: the site isn’t yet complete, so you’ll see lorem ipsum in place of book content 😄.]* We’ll go through setup, simple queries, complex queries, auth, and mutations for creating, updating, and deleting. Then we’ll cover advanced topics like infinite scrolling, local state, SSR, working offline, and performance. Here’s what it will look like:
6666

6767
![Guide app](img/guide-app.png)
6868

69-
React was released by Facebook in 2013, and it has since steadily increased in
70-
popularity, surpassing Angular in GitHub stars in 2016 to become the most
71-
popular Javascript view library. It continues to be developed by a team at
72-
Facebook, who have merged in contributions from over one thousand community
73-
members.
74-
7569
# Setting up
7670

7771
Section contents:
@@ -2529,7 +2523,7 @@ Section.propTypes = {
25292523
25302524
We give `this.props.viewedSection()` the section id mutation variable. We put it in a timeout so that we have time to scroll down to the bottom of the section to see the count change (End key or Cmd-⬇️ on Mac). And we clear the timeout on unmount (because if we navigate away, for example to our profile, and our timeout still fires, it would call a mutation provided by a `<Mutation>` component that no longer existed, and React would throw an error).
25312525
2532-
We also need to only trigger the mutation when the section changed. When the mutation result arrives and updates the Apollo store, `<Section>` is going to be given the updated `section` prop, so `componentDidUpdate()` will be called again. And if it always called `viewedSection()`, we’d be in an infinite loop. (Read: Author Loren was stuck in an infinite loop 😆.)
2526+
We also need to only trigger the mutation when the section changed. When the mutation result arrives and updates the Apollo store, `<Section>` is going to be given the updated `section` prop, so `componentDidUpdate()` will be called again. And if it always called `viewedSection()`, we’d be in an infinite loop. (Read: author Loren was stuck in an infinite loop 😆.)
25332527
25342528
![Infinite prop-updating loop](/img/infinite-loop.gif)
25352529
[*gif: Infinite prop-updating loop*](http://res.cloudinary.com/graphql/guide/infinite-loop.gif)
@@ -4300,13 +4294,21 @@ Section contents:
43004294
* [Cursors](6.md#cursors)
43014295
* [after](6.md#after)
43024296
* [orderBy](6.md#orderby)
4303-
* [Updating multiple queries]()
4304-
* [Local state]()
4305-
* [withUser]()
4306-
* [Reviews subscription]()
4307-
* [Batching]()
4308-
* [Prefetching]()
4309-
* [Persisting]()
4297+
* [Updating multiple queries](6.md#updating-multiple-queries)
4298+
* [Local state](6.md#local-state)
4299+
* [Direct writes](6.md#direct-writes)
4300+
* [Local mutations](6.md#local-mutations)
4301+
* [REST](6.md#rest)
4302+
* [Reviews subscriptions](6.md#reviews-subscriptions)
4303+
* [Subscription component](6.md#subscription-component)
4304+
* [Add new reviews](6.md#add-new-reviews)
4305+
* [Update on edit and delete](6.md#update-on-edit-and-delete)
4306+
* [Prefetching](6.md#prefetching)
4307+
* [On mouseover](6.md#on-mouseover)
4308+
* [Cache redirects](6.md#cache-redirects)
4309+
* [Batching](6.md#batching)
4310+
* [Persisting](6.md#persisting)
4311+
* [Multiple endpoints](6.md#multiple-endpoints)
43104312

43114313
## Paginating
43124314

@@ -5268,6 +5270,11 @@ Depending on what the query variables are (`orderBy`, `minStars`, and `minSenten
52685270
52695271
## Local state
52705272
5273+
Section contents:
5274+
5275+
* [Direct writes](6.md#direct-writes)
5276+
* [Local mutations](6.md#local-mutations)
5277+
52715278
In most of the apps we build, the majority of our *state* (the data that backs our UI) is *remote state*—it comes from a server and is saved in a database. But some of our state doesn’t come from the server and isn’t stored in a database—it originates locally on the client and stays there. This type of data is called our *local state*. One example of local state is a user setting that for whatever reason we didn’t want to send to the server to persist. Another example is data from a device API: if we were making a navigation app, we would want to display the device’s location and speed. A simple solution would be to put the state in a variable, for instance `window.gps`:
52725279
52735280
```js
@@ -6101,6 +6108,13 @@ To recap, we added `@rest` to our query, which made our REST link intercept the
61016108
61026109
> If you’re jumping in here, `git checkout 21_0.2.0` (tag [21_0.2.0](https://github.com/GraphQLGuide/guide/tree/21_0.2.0), or compare [21...22](https://github.com/GraphQLGuide/guide/compare/21_0.2.0...22_0.2.0))
61036110
6111+
Section contents:
6112+
6113+
* [Subscription component](6.md#subscription-component)
6114+
* [Add new reviews](6.md#add-new-reviews)
6115+
* [Update on edit and delete](6.md#update-on-edit-and-delete)
6116+
6117+
61046118
Early on in this chapter we set up our [first subscription](#subscriptions) for an updated GitHub star count. That was a very simple example—each event we received from the server contained a single integer:
61056119
61066120
```gql
@@ -6430,6 +6444,11 @@ For review updates, we replace the review in the list from the store (`prev`) wi
64306444
64316445
Background: [browser performance](bg.md#browser-performance)
64326446
6447+
Section contents:
6448+
6449+
* [On mouseover](6.md#on-mouseover)
6450+
* [Cache redirects](6.md#cache-redirects)
6451+
64336452
Prefetching is fetching data from the server before we need it so that when we do need it, we already have it on the client and can use it right away. This is great for UX because the user doesn’t have to look at a loading screen waiting for data to load. It’s a common pattern—both [Gatsby](https://www.gatsbyjs.org/docs/gatsby-link/) and [Next.js](https://nextjs.org/docs/#prefetching-pages) prefetch entire web pages with their `<Link>` components.
64346453
64356454
The most useful thing to prefetch in our app is the section content! We can prefetch just by making a query with the Apollo client:
@@ -7348,6 +7367,15 @@ We’re done! We can add more SpaceX data to different parts of our app by impor
73487367

73497368
# Extended topics
73507369

7370+
Section contents:
7371+
7372+
* [Linting](6.md#linting)
7373+
* [Setting up linting](6.md#setting-up-linting)
7374+
* [Fixing linting errors](6.md#fixing-linting-errors)
7375+
* [Using linting](6.md#using-linting)
7376+
* [Uploading files](6.md#uploading-files)
7377+
* [Testing](6.md#testing)
7378+
73517379
## Linting
73527380

73537381
> If you’re jumping in here, `git checkout 26_0.2.0` (tag [26_0.2.0](https://github.com/GraphQLGuide/guide/tree/26_0.2.0), or compare [26...27](https://github.com/GraphQLGuide/guide/compare/26_0.2.0...27_0.2.0))
@@ -7842,12 +7870,4 @@ Our server needs to support the [GraphQL multipart request spec](https://github.
78427870

78437871
We’re holding off on writing this section until the hooks+suspense version of React Apollo comes out. For now, we recommend the built-in [`<MockedProvider>`](https://www.apollographql.com/docs/react/recipes/testing.html) for the easiest setup or [this approach](https://medium.freecodecamp.org/a-new-approach-to-mocking-graphql-data-1ef49de3d491) for the most succinct test code.
78447872

7845-
We also recommend using [Jest](https://jestjs.io/) and [`react-testing-library`](https://testing-library.com/react). If you’d like a video introduction to them, as well as testing in general, we recommend [this course](https://testingjavascript.com/).
7846-
7847-
---
7848-
7849-
That’s all we have for now! We’re currently working on the server chapter. Please drop us a line if you have any suggestions for us 😊.
7850-
7851-
- <authors@graphql.guide>
7852-
- [GitHub issues: book text](https://github.com/GraphQLGuide/book/issues)
7853-
- [GitHub issues: code](https://github.com/GraphQLGuide/guide/issues)
7873+
We also recommend using [Jest](https://jestjs.io/) and [`react-testing-library`](https://testing-library.com/react). If you’d like a video introduction to them, as well as testing in general, we recommend [this course](https://testingjavascript.com/).

text/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ We have a [Background](bg.md) chapter that provides concise introductions to var
3434
Here’s a full list of the topics:
3535

3636
* [JavaScript](bg.md#javascript)
37+
* [JavaScript classes](bg.md#javascript-classes)
3738
* [JSON](bg.md#json)
3839
* [Git](bg.md#git)
3940
* [Node & npm & nvm](bg.md#node-&-npm-&-nvm)
4041
* [HTTP](bg.md#http)
42+
* [Server](bg.md#server)
43+
* [MongoDB](bg.md#mongodb)
4144
* [SPA](bg.md#spa)
4245
* [SSR](bg.md#ssr)
46+
* [React](bg.md#react)
4347
* [Latency](bg.md#latency)
4448
* [Webhooks](bg.md#webhooks)
49+
* [Testing](bg.md#testing)
4550
* [Continuous integration](bg.md#continuous-integration)
4651
* [Authentication](bg.md#authentication)
4752
* [Tokens vs. sessions](bg.md#tokens-vs-sessions)
@@ -140,9 +145,9 @@ Another important resource is the docs! Here they are for each library:
140145

141146
# Version
142147

143-
Book version: `r3` ([changelog](https://github.com/GraphQLGuide/book/releases))
148+
Book version: `r4` ([changelog](https://github.com/GraphQLGuide/book/releases))
144149

145-
Published April 10, 2019
150+
Published TODO September 13, 2019
146151

147152
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)).
148153

@@ -155,3 +160,11 @@ react-apollo 2.5
155160
graphql 0.14
156161
react 16.8
157162
```
163+
164+
## Chapter 11
165+
166+
Code version: `0.1.0` ([changelog](https://github.com/GraphQLGuide/guide-api/blob/master/CHANGELOG.md))
167+
168+
```
169+
apollo-server 2.6
170+
```

0 commit comments

Comments
 (0)