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: src/content/8/en/part8e.md
+16-17Lines changed: 16 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ lang: en
6
6
---
7
7
<divclass="content">
8
8
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.
10
10
11
11
### Fragments
12
12
@@ -135,10 +135,9 @@ Technically speaking, the HTTP protocol is not well-suited for communication fro
135
135
136
136
### Refactoring the backend
137
137
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.
139
139
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>
142
141
143
142
```js
144
143
consttypeDefs=`
@@ -325,7 +324,7 @@ const resolvers = {
325
324
module.exports= resolvers
326
325
```
327
326
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:
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.
351
350
352
351
Let us install Express
353
352
@@ -438,7 +437,7 @@ There are several changes to the code. [ApolloServerPluginDrainHttpServer](https
438
437
439
438
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.
440
439
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.
442
441
443
442
The backend code can be found on [GitHub](https://github.com/fullstack-hy2020/graphql-phonebook-backend/tree/part8-6), branch <i>part8-6</i>.
444
443
@@ -462,7 +461,7 @@ First, we have to install two packages for adding subscriptions to GraphQL and a
462
461
npm install graphql-ws ws @graphql-tools/schema
463
462
```
464
463
465
-
The file <i>index.js</i> is changed to
464
+
The file <i>index.js</i> is changed to:
466
465
467
466
```js
468
467
// highlight-start
@@ -538,7 +537,7 @@ start()
538
537
539
538
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).
540
539
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.
542
541
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.
543
542
544
543
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 = {
599
598
}
600
599
```
601
600
602
-
The following library needs to be installed
601
+
The following library needs to be installed:
603
602
604
603
```bash
605
604
npm install graphql-subscriptions
606
605
```
607
606
608
607
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).
609
608
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
611
610
["iterator object"](https://www.apollographql.com/docs/apollo-server/data/subscriptions/#listening-for-events) called <i>PERSON\_ADDED</i> thanks to the following code:
612
611
613
612
```js
@@ -618,7 +617,7 @@ Subscription: {
618
617
},
619
618
```
620
619
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.
622
621
623
622
Adding a new person <i>publishes</i> a notification about the operation to all subscribers with PubSub's method *publish*:
624
623
@@ -632,19 +631,19 @@ It's possible to test the subscriptions with the Apollo Explorer like this:
632
631
633
632

634
633
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.
636
635
637
636
If the subscription does not work, check that you have correct connection settings:
638
637
639
638

640
639
641
640
The backend code can be found on [GitHub](https://github.com/fullstack-hy2020/graphql-phonebook-backend/tree/part8-7), branch <i>part8-7</i>.
642
641
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.
644
643
645
644
### Subscriptions on the client
646
645
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/).
648
647
The configuration in <i>index.js</i> has to be modified like so:
649
648
650
649
```js
@@ -724,7 +723,7 @@ const wsLink = new GraphQLWsLink(
724
723
725
724
The subscriptions are done using the [useSubscription](https://www.apollographql.com/docs/react/api/react/hooks/#usesubscription) hook function.
726
725
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>:
728
727
729
728
```js
730
729
// highlight-start
@@ -789,7 +788,7 @@ const App = () => {
789
788
}
790
789
```
791
790
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.
793
792
794
793
Let us now fix the problem by ensuring that a person is not added twice in the cache:
0 commit comments