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/part8a.md
+21-35Lines changed: 21 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -250,17 +250,17 @@ Let's implement a GraphQL server with today's leading library: [Apollo Server](h
250
250
Create a new npm project with _npm init_ and install the required dependencies.
251
251
252
252
```bash
253
-
npm install apollo-server@3.10.1 graphql
253
+
npm install @apollo/server graphql
254
254
```
255
255
256
-
**Note** at the time of writing (10th Dec 2022) the code used in this part is not fully compatible with the new version of the Apollo Server, and because of this, if you want everything to work smoothly you should install the version _3.10.1_. Material shall be updated to use the most recent Apollo Server in early 2023.
257
256
258
257
Also create a `index.js` file in your project's root directory.
@@ -315,12 +315,14 @@ const server = new ApolloServer({
315
315
resolvers,
316
316
})
317
317
318
-
server.listen().then(({ url }) => {
318
+
startStandaloneServer(server, {
319
+
listen: { port:4000 },
320
+
}).then(({ url }) => {
319
321
console.log(`Server ready at ${url}`)
320
322
})
321
323
```
322
324
323
-
The heart of the code is an _ApolloServer_, which is given two parameters:
325
+
The heart of the code is an [ApolloServer](https://www.apollographql.com/docs/apollo-server/api/apollo-server/), which is given two parameters:
324
326
325
327
```js
326
328
constserver=newApolloServer({
@@ -331,7 +333,7 @@ const server = new ApolloServer({
331
333
332
334
The first parameter, _typeDefs_, contains the GraphQL schema.
333
335
334
-
The second parameter is an object, which contains the [resolvers](https://www.apollographql.com/tutorials/fullstack-quickstart/04-writing-query-resolvers) of the server. These are the code, which defines <i>how</i> GraphQL queries are responded to.
336
+
The second parameter is an object, which contains the [resolvers](https://www.apollographql.com/docs/apollo-server/data/resolvers/) of the server. These are the code, which defines <i>how</i> GraphQL queries are responded to.
335
337
336
338
The code of the resolvers is the following:
337
339
@@ -426,13 +428,10 @@ The second parameter, _args_, contains the parameters of the query.
426
428
The resolver then returns from the array _persons_ the person whose name is the same as the value of <i>args.name</i>.
427
429
The resolver does not need the first parameter _root_.
428
430
429
-
430
-
431
-
In fact, all resolver functions are given [four parameters](https://www.graphql-tools.com/docs/resolvers#resolver-function-signature). With JavaScript, the parameters don't have to be defined if they are not needed. We will be using the first and the third parameter of a resolver later in this part.
431
+
In fact, all resolver functions are given [four parameters](https://www.graphql-tools.com/docs/resolvers#resolver-function-signature). With JavaScript, the parameters don't have to be defined if they are not needed. We will be using the first and the third parameter of a resolver later in this part.
432
432
433
433
### The default resolver
434
434
435
-
436
435
When we do a query, for example
437
436
438
437
```js
@@ -453,7 +452,6 @@ We have so far only defined resolvers for fields of the type <i>Query</i>, so fo
453
452
Because we did not define resolvers for the fields of the type <i>Person</i>, Apollo has defined [default resolvers](https://www.graphql-tools.com/docs/resolvers/#default-resolver) for them.
454
453
They work like the one shown below:
455
454
456
-
457
455
```js
458
456
constresolvers= {
459
457
Query: {
@@ -473,13 +471,10 @@ const resolvers = {
473
471
}
474
472
```
475
473
476
-
477
474
The default resolver returns the value of the corresponding field of the object. The object itself can be accessed through the first parameter of the resolver, _root_.
478
475
479
-
480
476
If the functionality of the default resolver is enough, you don't need to define your own. It is also possible to define resolvers for only some fields of a type, and let the default resolvers handle the rest.
481
477
482
-
483
478
We could for example define that the address of all persons is
484
479
<i>Manhattan New York</i> by hard-coding the following to the resolvers of the street and city fields of the type <i>Person</i>:
485
480
@@ -516,10 +511,8 @@ type Query {
516
511
}
517
512
```
518
513
519
-
520
514
so a person now has a field with the type <i>Address</i>, which contains the street and the city.
521
515
522
-
523
516
The queries requiring the address change into
524
517
525
518
```js
@@ -565,7 +558,6 @@ let persons = [
565
558
]
566
559
```
567
560
568
-
569
561
The person-objects saved in the server are not exactly the same as the GraphQL type <i>Person</i> objects described in the schema.
570
562
571
563
Contrary to the <i>Person</i> type, the <i>Address</i> type does not have an <i>id</i> field, because they are not saved into their own separate data structure in the server.
@@ -615,10 +607,8 @@ type Mutation {
615
607
}
616
608
```
617
609
618
-
619
610
The Mutation is given the details of the person as parameters. The parameter <i>phone</i> is the only one which is nullable. The Mutation also has a return value. The return value is type <i>Person</i>, the idea being that the details of the added person are returned if the operation is successful and if not, null. Value for the field <i>id</i> is not given as a parameter. Generating an id is better left for the server.
620
611
621
-
622
612
Mutations also require a resolver:
623
613
624
614
```js
@@ -707,14 +697,13 @@ If we try to create a new person, but the parameters do not correspond with the
707
697
708
698
So some of the error handling can be automatically done with GraphQL [validation](https://graphql.org/learn/validation/).
709
699
710
-
However, GraphQL cannot handle everything automatically. For example, stricter rules for data sent to a Mutation have to be added manually.
711
-
The errors from those rules are handled by [the error handling mechanism of Apollo Server](https://www.apollographql.com/docs/apollo-server/data/errors).
700
+
However, GraphQL cannot handle everything automatically. For example, stricter rules for data sent to a Mutation have to be added manually. An error could be handeled by throwing [GraphQLError](https://www.apollographql.com/docs/apollo-server/data/errors/#custom-errors) with a proper
So if the name to be added already exists in the phonebook, throw _UserInputError_ error.
734
+
So if the name to be added already exists in the phonebook, throw _GraphQLError_ error.
743
735
744
-

736
+

745
737
746
738
The current code of the application can be found on [GitHub](https://github.com/fullstack-hy2020/graphql-phonebook-backend/tree/part8-2), branch <i>part8-2</i>.
747
739
@@ -933,12 +925,6 @@ In some cases, it might be beneficial to name the queries. This is the case espe
933
925
Through the exercises, we will implement a GraphQL backend for a small library.
934
926
Start with [this file](https://github.com/fullstack-hy2020/misc/blob/master/library-backend.js). Remember to _npm init_ and to install dependencies!
935
927
936
-
**Note** at the time of writing (10th Dec 2022) the code used in this part is not fully compatible with the new version of the Apollo Server, and because of this, if you want everything to work smoothly you should install the version _3.10.1_:
Surprisingly, when a person's number is changed, the new number automatically appears on the list of persons rendered by the <i>Persons</i> component.
751
746
This happens because each person has an identifying field of type <i>ID</i>, so the person's details saved to the cache update automatically when they are changed with the mutation.
752
747
753
-
The current code of the application can be found on [GitHub](https://github.com/fullstack-hy2020/graphql-phonebook-frontend/tree/part8-4) branch <i>part8-4</i>.
754
-
755
748
Our application still has one small flaw. If we try to change the phone number for a name which does not exist, nothing seems to happen.
756
749
This happens because if a person with the given name cannot be found,
757
750
the mutation response is <i>null</i>:
@@ -820,7 +813,7 @@ useEffect(() => {
820
813
821
814
However, this solution does not work if the _notify_ function is not wrapped to a [useCallback](https://reactjs.org/docs/hooks-reference.html#usecallback) function. If it's not, this results in an endless loop. When the _App_ component is rerendered after a notification is removed, a <i>new version</i> of _notify_ gets created which causes the effect function to be executed, which causes a new notification, and so on, and so on...
822
815
823
-
The current code of the application can be found on [GitHub](https://github.com/fullstack-hy2020/graphql-phonebook-frontend/tree/part8-5) branch <i>part8-5</i>.
816
+
The current code of the application can be found on [GitHub](https://github.com/fullstack-hy2020/graphql-phonebook-frontend/tree/part8-4) branch <i>part8-4</i>.
Copy file name to clipboardExpand all lines: src/content/8/fi/osa8a.md
+18-21Lines changed: 18 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -245,15 +245,14 @@ Toteutetaan nyt GraphQL-palvelin tämän hetken johtavaa kirjastoa [Apollo Serve
245
245
Luodaan uusi npm-projekti komennolla _npm init_ ja asennetaan tarvittavat riippuvuudet
246
246
247
247
```bash
248
-
npm install apollo-server@3.10.1 graphql
248
+
npm install @apollo/server graphql
249
249
```
250
250
251
-
**Huom:** tätä kirjoittaessa (10.12.2022) tämän osan koodi ei ole täysin yhteensopivaa uusimman Apollo Serverin version kanssa. Tämän takia kannattaa asentaa versio _3.10.1_ jos haluat että koodi toimii sellaisenaan. Osa päivitetään vuoden 2023 alkupuolella.
@@ -308,12 +307,14 @@ const server = new ApolloServer({
308
307
resolvers,
309
308
})
310
309
311
-
server.listen().then(({ url }) => {
310
+
startStandaloneServer(server, {
311
+
listen: { port:4000 },
312
+
}).then(({ url }) => {
312
313
console.log(`Server ready at ${url}`)
313
314
})
314
315
```
315
316
316
-
Toteutuksen ytimessä on _ApolloServer_, joka saa kaksi parametria
317
+
Toteutuksen ytimessä on [ApolloServer](https://www.apollographql.com/docs/apollo-server/api/apollo-server/), joka saa kaksi parametria
317
318
318
319
```js
319
320
constserver=newApolloServer({
@@ -324,7 +325,7 @@ const server = new ApolloServer({
324
325
325
326
parametreista ensimmäinen _typeDefs_ sisältää sovelluksen käyttämän GraphQL-skeeman.
326
327
327
-
Toinen parametri on olio, joka sisältää palvelimen [resolverit](https://www.apollographql.com/docs/apollo-server/data/data/#resolver-map), eli käytännössä koodin, joka määrittelee <i>miten</i> GraphQL-kyselyihin vastataan.
328
+
Toinen parametri on olio, joka sisältää palvelimen [resolverit](https://www.apollographql.com/docs/apollo-server/data/resolvers/), eli käytännössä koodin, joka määrittelee <i>miten</i> GraphQL-kyselyihin vastataan.
328
329
329
330
Resolverien koodi on seuraavassa:
330
331
@@ -698,12 +699,12 @@ Jos yritämme luoda uuden henkilön, mutta parametrit eivät vastaa skeemassa m
698
699
699
700
GraphQL:n[validoinnin](https://graphql.org/learn/validation/) avulla pystytään siis jo automaattisesti hoitamaan osa virheenkäsittelyä.
700
701
701
-
Kaikkea GraphQL ei kuitenkaan pysty hoitamaan automaattisesti. Esimerkiksi tarkemmat säännöt mutaatiolla lisättävän datan kenttien muodolle on lisättävä itse. Niistä aiheutuvat virheet tulee hoitaa [GraphQL:n poikkeuskäsittelymekanismilla](https://www.apollographql.com/docs/apollo-server/data/errors/).
702
+
Kaikkea GraphQL ei kuitenkaan pysty hoitamaan automaattisesti. Esimerkiksi tarkemmat säännöt mutaatiolla lisättävän datan kenttien muodolle on lisättävä itse. Niistä aiheutuvat virheet tulee hoitaa itse heittämällä sopivalla [virhekoodilla](https://www.apollographql.com/docs/apollo-server/data/errors/#built-in-error-codes) varustetu [GraphQLError](https://www.apollographql.com/docs/apollo-server/data/errors/#custom-errors).
702
703
703
704
Estetään saman nimen lisääminen puhelinluetteloon useampaan kertaan:
Eli jos lisättävä nimi on jo luettelossa heitetään poikkeus _UserInputError_.
734
+
Eli jos lisättävä nimi on jo luettelossa heitetään poikkeus _GraphQLError_.
731
735
732
-

736
+

733
737
734
738
Sovelluksen tämänhetkinen koodi on kokonaisuudessaan [GitHubissa](https://github.com/fullstack-hy2020/graphql-phonebook-backend/tree/part8-2), branchissa <i>part8-2</i>.
735
739
@@ -921,13 +925,6 @@ Tehtävissä toteutetaan yksinkertaisen kirjaston GraphQL:ää tarjoava backend.
921
925
922
926
Huomaa, että koodin käynnistäminen aiheuttaa alussa virheen, sillä skeeman määrittely on puutteellinen.
923
927
924
-
**Huom:** tätä kirjoittaessa (10.12.2022) tämän osan koodi ei ole täysin yhteensopivaa uusimman Apollo Serverin version kanssa. Tämän takia kannattaa asentaa versio _3.10.1_ jos haluat että koodi toimii sellaisenaan. Osa päivitetään vuoden 2023 alkupuolella.
0 commit comments