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-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -117,7 +117,7 @@ module.exports = { User }
117
117
118
118
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).
119
119
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):
121
121
122
122

123
123
*The two records stored in the “users” collection of the MongoDB database.*
Copy file name to clipboardExpand all lines: text/5.md
+17-18Lines changed: 17 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ written these days follows the
22
22
in which one computer is always providing a service (a **server**), and another
23
23
computer is always requesting the service (a **client**, such as a web browser or mobile app). In a GraphQL client–server model, the client
24
24
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
26
26
in the [last](11.md).
27
27
28
28
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/),
38
38
Native.
39
39
40
40
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
42
42
GraphQL Guide.
43
43
44
44
# Anywhere: HTTP
@@ -53,7 +53,7 @@ request.
53
53
## cURL
54
54
55
55
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
57
57
requests, including HTTP requests):
58
58
59
59
```sh
@@ -171,7 +171,7 @@ languages. Here is some common functionality that the libraries might provide:
171
171
-[Caching](#caching)
172
172
-[DevTools](#devtools)
173
173
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.
175
175
176
176
## Streamlined request function
177
177
@@ -262,11 +262,11 @@ Our query returns:
262
262
263
263
And our library saves the response in a normalized cache. The key to each object
264
264
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:
266
266
267
267
```js
268
268
cache = {
269
-
"User:1" {
269
+
"User:1": {
270
270
id:"1",
271
271
firstName:"Loren",
272
272
hasRead: [
@@ -275,12 +275,12 @@ cache = {
275
275
]
276
276
},
277
277
"Section:5_1": {
278
-
"id":"5_1",
279
-
"title":"Anywhere: HTTP"
278
+
id:"5_1",
279
+
title:"Anywhere: HTTP"
280
280
},
281
281
"Section:5_2": {
282
-
"id":"5_2",
283
-
"title":"Client Libraries"
282
+
id:"5_2",
283
+
title:"Client Libraries"
284
284
}
285
285
}
286
286
```
@@ -306,9 +306,9 @@ Which should return:
306
306
}
307
307
```
308
308
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.
310
310
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
312
312
immediately return it to us.
313
313
314
314
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
317
317
while we wait for the response to arrive. The time it takes to get a value from
318
318
the cache and display it on the screen could be as little as 20ms, whereas
319
319
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
321
321
how long the server takes to retrieve their data. Humans
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.
327
327
328
328
## DevTools
329
329
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”:
331
331
332
332

333
333
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:
335
335
336
336

337
337
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:
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:
343
343
344
344

345
345
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.
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:
66
66
67
67

68
68
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
-
75
69
# Setting up
76
70
77
71
Section contents:
@@ -2529,7 +2523,7 @@ Section.propTypes = {
2529
2523
2530
2524
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).
2531
2525
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 😆.)
* [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)
4310
4312
4311
4313
## Paginating
4312
4314
@@ -5268,6 +5270,11 @@ Depending on what the query variables are (`orderBy`, `minStars`, and `minSenten
5268
5270
5269
5271
## Local state
5270
5272
5273
+
Section contents:
5274
+
5275
+
* [Direct writes](6.md#direct-writes)
5276
+
* [Local mutations](6.md#local-mutations)
5277
+
5271
5278
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`:
5272
5279
5273
5280
```js
@@ -6101,6 +6108,13 @@ To recap, we added `@rest` to our query, which made our REST link intercept the
6101
6108
6102
6109
> 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))
* [Update on edit and delete](6.md#update-on-edit-and-delete)
6116
+
6117
+
6104
6118
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:
6105
6119
6106
6120
```gql
@@ -6430,6 +6444,11 @@ For review updates, we replace the review in the list from the store (`prev`) wi
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.
6434
6453
6435
6454
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
> 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.
7842
7870
7843
7871
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.
7844
7872
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)
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/).
@@ -140,9 +145,9 @@ Another important resource is the docs! Here they are for each library:
140
145
141
146
# Version
142
147
143
-
Book version: `r3` ([changelog](https://github.com/GraphQLGuide/book/releases))
148
+
Book version: `r4` ([changelog](https://github.com/GraphQLGuide/book/releases))
144
149
145
-
Published April 10, 2019
150
+
Published TODO September 13, 2019
146
151
147
152
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)).
0 commit comments