Skip to content

Commit e4cd70c

Browse files
authored
Update part8e.md - Fix typos, punctuation. Improve phrasing
1 parent fd2a258 commit e4cd70c

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/content/8/en/part8e.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ lang: en
66
---
77
<div class="content">
88

9-
We are approaching the end of this part. Let's finish by having a look at a few more details of GraphQL.
9+
We are approaching the end of this part. Let's finish by having a look at a few more details about GraphQL.
1010

1111
### Fragments
1212

@@ -135,10 +135,9 @@ Technically speaking, the HTTP protocol is not well-suited for communication fro
135135

136136
### Refactoring the backend
137137

138-
Since version 3.0 Apollo Server does not support subscriptions out of the box so we need to do some changes before we set up subscriptions. Let us also clean the app structure a bit.
138+
Since version 3.0 Apollo Server does not support subscriptions out of the box, we need to do some changes before we set up subscriptions. Let us also clean the app structure a bit.
139139

140-
Let us start by extracting the schema definition to file
141-
<i>schema.js</i>
140+
Let's start by extracting the schema definition to the file <i>schema.js</i>
142141

143142
```js
144143
const typeDefs = `
@@ -325,7 +324,7 @@ const resolvers = {
325324
module.exports = resolvers
326325
```
327326

328-
So far, we have started the application with the easy-to-use function [startStandaloneServer](https://www.apollographql.com/docs/apollo-server/api/standalone/#startstandaloneserver), thanks to which the application has not had to be configured at much:
327+
So far, we have started the application with the easy-to-use function [startStandaloneServer](https://www.apollographql.com/docs/apollo-server/api/standalone/#startstandaloneserver), thanks to which the application has not had to be configured that much:
329328

330329
```js
331330
const { startStandaloneServer } = require('@apollo/server/standalone')
@@ -347,7 +346,7 @@ startStandaloneServer(server, {
347346
})
348347
```
349348

350-
Unfortunately startStandaloneServer does not allow adding subscriptions to the application, so let's switch to the more robust [expressMiddleware](https://www.apollographql.com/docs/apollo-server/api/express-middleware/) function. As the name of the function already suggests, it is an Express middleware, which means that Express must also be configured for the application, with the GraphQL server acting as middleware.
349+
Unfortunately, startStandaloneServer does not allow adding subscriptions to the application, so let's switch to the more robust [expressMiddleware](https://www.apollographql.com/docs/apollo-server/api/express-middleware/) function. As the name of the function already suggests, it is an Express middleware, which means that Express must also be configured for the application, with the GraphQL server acting as middleware.
351350

352351
Let us install Express
353352

@@ -438,7 +437,7 @@ There are several changes to the code. [ApolloServerPluginDrainHttpServer](https
438437
439438
The GraphQL server in the *server* variable is now connected to listen to the root of the server, i.e. to the */* route, using the *expressMiddleware* object. Information about the logged-in user is set in the context using the function we defined earlier. Since it is an Express server, the middlewares express-json and cors are also needed so that the data included in the requests is correctly parsed and so that CORS problems do not appear.
440439

441-
Since the GraphQL server must be started before the Express application can start listening to the specified port, the entire initialization has had to be placed in an <i>async function</i>, which allows waiting for the GraphQL server to start:
440+
Since the GraphQL server must be started before the Express application can start listening to the specified port, the entire initialization has had to be placed in an <i>async function</i>, which allows waiting for the GraphQL server to start.
442441

443442
The backend code can be found on [GitHub](https://github.com/fullstack-hy2020/graphql-phonebook-backend/tree/part8-6), branch <i>part8-6</i>.
444443

@@ -462,7 +461,7 @@ First, we have to install two packages for adding subscriptions to GraphQL and a
462461
npm install graphql-ws ws @graphql-tools/schema
463462
```
464463

465-
The file <i>index.js</i> is changed to
464+
The file <i>index.js</i> is changed to:
466465

467466
```js
468467
// highlight-start
@@ -538,7 +537,7 @@ start()
538537

539538
When queries and mutations are used, GraphQL uses the HTTP protocol in the communication. In case of subscriptions, the communication between client and server happens with [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API).
540539

541-
The above code registers a WebSocketServer object to listen the WebSocket connections, besides the usual HTTP connections that the server listens. The second part of the definition registers a function that closes the WebSocket connection on server shutdown.
540+
The above code registers a WebSocketServer object to listen the WebSocket connections, besides the usual HTTP connections that the server listens to. The second part of the definition registers a function that closes the WebSocket connection on server shutdown.
542541
If you're interested in more details about configurations, Apollo's [documentation](https://www.apollographql.com/docs/apollo-server/data/subscriptions) explains in relative detail what each line of code does.
543542

544543
WebSockets are a perfect match for communication in the case of GraphQL subscriptions since when WebSockets are used, also the server can initiate the communication.
@@ -599,15 +598,15 @@ const resolvers = {
599598
}
600599
```
601600

602-
The following library needs to be installed
601+
The following library needs to be installed:
603602

604603
```bash
605604
npm install graphql-subscriptions
606605
```
607606

608607
With subscriptions, the communication happens using the [publish-subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) principle utilizing the object [PubSub](https://www.apollographql.com/docs/apollo-server/data/subscriptions#the-pubsub-class).
609608

610-
There is only few lines of code added, but quite much is happening under the hood. The resolver of the *personAdded* subscription registers and saves info about all the clients that do the subscription. The clients are saved to an
609+
There are only a few lines of code added, but quite a lot is happening under the hood. The resolver of the *personAdded* subscription registers and saves info about all the clients that do the subscription. The clients are saved to an
611610
["iterator object"](https://www.apollographql.com/docs/apollo-server/data/subscriptions/#listening-for-events) called <i>PERSON\_ADDED</i> thanks to the following code:
612611

613612
```js
@@ -618,7 +617,7 @@ Subscription: {
618617
},
619618
```
620619

621-
The iterator name is an arbitrary string, now the name follows the convention, it is the subscription name written in capital letters.
620+
The iterator name is an arbitrary string, but to follow the convention, it is the subscription name written in capital letters.
622621

623622
Adding a new person <i>publishes</i> a notification about the operation to all subscribers with PubSub's method *publish*:
624623

@@ -632,19 +631,19 @@ It's possible to test the subscriptions with the Apollo Explorer like this:
632631

633632
![apollo explorer showing subscriptions tab and response](../../images/8/31x.png)
634633

635-
When the blue button <i>PersonAdded</i> is pressed Explorer starts to wait for a new person to be added. On addition (that you need to do from another browser window) the info of the added person appears in the right side of the Explorer.
634+
When the blue button <i>PersonAdded</i> is pressed, Explorer starts to wait for a new person to be added. On addition (that you need to do from another browser window), the info of the added person appears in the right side of the Explorer.
636635

637636
If the subscription does not work, check that you have correct connection settings:
638637

639638
![apollo studio showing cog red arrow highlighting](../../images/8/35.png)
640639

641640
The backend code can be found on [GitHub](https://github.com/fullstack-hy2020/graphql-phonebook-backend/tree/part8-7), branch <i>part8-7</i>.
642641

643-
Implementing subscriptions involves a lot of configurations. You will be able to cope with the few exercises of this course without worrying much about the details. If you planning to use subsctiptions in an production use application, you should definitely read carefully Apollo's [documentation on subscriptions](https://www.apollographql.com/docs/apollo-server/data/subscriptions).
642+
Implementing subscriptions involves a lot of configurations. You will be able to cope with the few exercises of this course without worrying much about the details. If you are planning to use subsctiptions in an production use application, you should definitely read Apollo's [documentation on subscriptions](https://www.apollographql.com/docs/apollo-server/data/subscriptions) carefully.
644643

645644
### Subscriptions on the client
646645

647-
In order to use subscriptions in our React application, we have to do some changes, especially on its [configuration](https://www.apollographql.com/docs/react/data/subscriptions/).
646+
In order to use subscriptions in our React application, we have to do some changes, especially to its [configuration](https://www.apollographql.com/docs/react/data/subscriptions/).
648647
The configuration in <i>index.js</i> has to be modified like so:
649648

650649
```js
@@ -724,7 +723,7 @@ const wsLink = new GraphQLWsLink(
724723

725724
The subscriptions are done using the [useSubscription](https://www.apollographql.com/docs/react/api/react/hooks/#usesubscription) hook function.
726725

727-
Let's make the following changes to the code. Add the code defining the order to the file <i>queries.js</i>:
726+
Let's make the following changes to the code. Add the code defining the subscription to the file <i>queries.js</i>:
728727

729728
```js
730729
// highlight-start
@@ -789,7 +788,7 @@ const App = () => {
789788
}
790789
```
791790

792-
Our solution has a small problem: a person is added to the cache and also rendered twice since the component *PersonForm* is also adding it to the cache.
791+
Our solution has a small problem: a person is added to the cache and also rendered twice since the component *PersonForm* is adding it to the cache as well.
793792

794793
Let us now fix the problem by ensuring that a person is not added twice in the cache:
795794

0 commit comments

Comments
 (0)