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/0/en/part0a.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,8 @@ Participants are expected to have good programming skills, basic knowledge of we
17
17
18
18
Previous knowledge of JavaScript or other course topics is not required.
19
19
20
+
How much programming experience is needed? It is hard to say, but you should be pretty fluent with <i>your</i> language. This level of fluency takes usually at least 100-200 of hours practice to develop.
21
+
20
22
### Course material
21
23
22
24
The course material is meant to be read one part at a time and in order.
The new user mutation is straightforward. The login mutation checks if the username/password pair is valid. And if it is indeed valid, it returns a jwt token familiar from [part 4](/en/part4/token_authentication).
311
-
315
+
The new user mutation is straightforward. The login mutation checks if the username/password pair is valid. And if it is indeed valid, it returns a jwt token familiar from [part 4](/en/part4/token_authentication). Note that the *JWT\_SECRET* must be defined in the <i>.env</i> file.
312
316
313
317
User creation is done now as follows:
314
318
@@ -342,25 +346,26 @@ In the Apollo Explorer, the header is added to a query like so:
342
346
343
347

344
348
345
-
Let's now expand the definition of the _server_ object by adding a third parameter [context](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) to the constructor call:
349
+
Modify the startup of the backend by giving the function that handles the startup [startStandaloneServer](https://www.apollographql.com/docs/apollo-server/api/standalone/) another parameter [context](https://www.apollographql.com /docs/apollo-server/data/context/)
346
350
347
351
```js
348
-
constserver=newApolloServer({
349
-
typeDefs,
350
-
resolvers,
352
+
startStandaloneServer(server, {
353
+
listen: { port:4000 },
351
354
// highlight-start
352
-
context:async ({ req }) => {
355
+
context:async ({ req, res }) => {
353
356
constauth= req ?req.headers.authorization:null
354
-
if (auth &&auth.toLowerCase().startsWith('bearer')) {
If a logged-in user cannot be found from the context, an _AuthenticationError_ is thrown. Creating new persons is now done with _async/await_ syntax, because if the operation is successful, the created person is added to the friends list of the user.
434
+
If a logged-in user cannot be found from the context, an _GraphQLError_ with a proper message is thrown. Creating new persons is now done with _async/await_ syntax, because if the operation is successful, the created person is added to the friends list of the user.
419
435
420
436
Let's also add functionality for adding an existing user to your friends list. The mutation is as follows:
Copy file name to clipboardExpand all lines: src/content/8/en/part8d.md
+12-9Lines changed: 12 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -159,13 +159,12 @@ const App = () => {
159
159
}
160
160
```
161
161
162
-
The current code of the application can be found on [Github](https://github.com/fullstack-hy2020/graphql-phonebook-frontend/tree/part8-6), branch <i>part8-6</i>.
163
-
164
162
### Adding a token to a header
165
163
166
164
After the backend changes, creating new persons requires that a valid user token is sent with the request. In order to send the token, we have to change the way we define the _ApolloClient_ object in <i>index.js</i> a little.
The link parameter given to the _client_ object defines how apollo connects to the server. Here, the normal [httpLink](https://www.apollographql.com/docs/link/links/http.htm)connection is modified so that the request's <i>authorization</i> [header](https://www.apollographql.com/docs/react/networking/authentication/#header)contains the token if one has been saved to the localStorage.
192
+
The field _uri_ that was previously used when creating the _client_ object has been replaced by the field _link_, which defines in a more complicated case how Apollo is connected to the server. The server url is now wrapped using the function [createHttpLink](https://www.apollographql.com/docs/link/links/http.htm)into a suitable httpLink object. The link is modified by the [context](https://www .apollographql.com/docs/react/api/link/apollo-link-context/#overview) defined by the authLink object so that a possible token in localStorage is [set to header](https://www.apollographql.com/docs/react/networking/authentication/#header) <i>authorization</i> for each request to the server.
192
193
193
194
Creating new persons and changing numbers works again. There is however one remaining problem. If we try to add a person without a phone number, it is not possible.
This approach is pretty good, the drawback being that the query is always rerun with any updates.
240
243
241
-
It is possible to optimize the solution by handling updating the cache ourselves. This is done by defining a suitable [update](https://www.apollographql.com/docs/react/data/mutations/#update) callback for the mutation, which Apollo runs after the mutation:
244
+
It is possible to optimize the solution by handling updating the cache ourselves. This is done by defining a suitable [update](https://www.apollographql.com/docs/react/data/mutations/#the-update-function) callback for the mutation, which Apollo runs after the mutation:
242
245
243
246
```js
244
247
constPersonForm= ({ setError }) => {
@@ -276,7 +279,7 @@ Be diligent with the cache. Old data in cache can cause hard-to-find bugs. As we
276
279
277
280
> <i>There are only two hard things in Computer Science: cache invalidation and naming things.</i> Read more [here](https://martinfowler.com/bliki/TwoHardThings.html).
278
281
279
-
The current code of the application can be found on [Github](https://github.com/fullstack-hy2020/graphql-phonebook-frontend/tree/part8-8), branch <i>part8-8</i>.
282
+
The current code of the application can be found on [Github](https://github.com/fullstack-hy2020/graphql-phonebook-frontend/tree/part8-7), branch <i>part8-7</i>.
0 commit comments