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/part8c.md
+24-12Lines changed: 24 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,10 +11,10 @@ We will now add user management to our application, but let's first start using
11
11
12
12
### Mongoose and Apollo
13
13
14
-
Install mongoose:
14
+
Install Mongoose and dotenv:
15
15
16
16
```bash
17
-
npm install mongoose
17
+
npm install mongoose dotenv
18
18
```
19
19
20
20
We will imitate what we did in parts [3](/en/part3/saving_data_to_mongo_db) and [4](/en/part4/structure_of_backend_application_introduction_to_testing).
@@ -55,11 +55,12 @@ We also included a few validations. _required: true_, which makes sure that a va
55
55
We can get the application to mostly work with the following changes:
As well as in GraphQL, the input is now validated using the validations defined in the mongoose schema. For handling possible validation errors in the schema, we must add an error-handling _try/catch_ block to the _save_ method. When we end up in the catch, we throw a suitable exception:
160
+
As well as in GraphQL, the input is now validated using the validations defined in the mongoose schema. For handling possible validation errors in the schema, we must add an error-handling _try/catch_ block to the _save_ method. When we end up in the catch, we throw a exception[GraphQLError](https://www.apollographql.com/docs/apollo-server/data/errors/#custom-errors) with error code :
159
161
160
162
```js
161
163
Mutation: {
@@ -166,8 +168,12 @@ Mutation: {
166
168
try {
167
169
awaitperson.save()
168
170
} catch (error) {
169
-
thrownewUserInputError(error.message, {
170
-
invalidArgs: args,
171
+
thrownewGraphQLError('Saving person failed', {
172
+
extensions: {
173
+
code:'BAD_USER_INPUT',
174
+
invalidArgs:args.name,
175
+
error
176
+
}
171
177
})
172
178
}
173
179
// highlight-end
@@ -182,8 +188,12 @@ Mutation: {
182
188
try {
183
189
awaitperson.save()
184
190
} catch (error) {
185
-
thrownewUserInputError(error.message, {
186
-
invalidArgs: args,
191
+
thrownewGraphQLError('Saving number failed', {
192
+
extensions: {
193
+
code:'BAD_USER_INPUT',
194
+
invalidArgs:args.name,
195
+
error
196
+
}
187
197
})
188
198
}
189
199
// highlight-end
@@ -193,6 +203,8 @@ Mutation: {
193
203
}
194
204
```
195
205
206
+
We have also added the Mongoose error and the data that caused the error to the <i>extensions</i> object that is used to convey more info about the cause of the error to the caller.
207
+
196
208
The code of the backend can be found on [Github](https://github.com/fullstack-hy2020/graphql-phonebook-backend/tree/part8-4), branch <i>part8-4</i>.
console.log('error connection to MongoDB:', error.message)
71
74
})
72
75
73
-
consttypeDefs=gql`
76
+
consttypeDefs=`
74
77
...
75
78
`
76
79
@@ -109,21 +112,21 @@ Muutokset ovat melko suoraviivaisia. Huomio kiinnittyy pariin seikkaan. Kuten mu
109
112
110
113
Toinen huomionarvoinen seikka on se, että resolverifunktiot palauttavat nyt <i>promisen</i>, aiemminhan ne palauttivat aina normaaleja oliota. Kun resolveri palauttaa promisen, Apollo server [osaa lähettää vastaukseksi](https://www.apollographql.com/docs/apollo-server/data/resolvers/#return-values) sen arvon mihin promise resolvoituu.
111
114
112
-
113
115
Eli esimerkiksi jos seuraava resolverifunktio suoritetaan,
114
116
115
117
```js
116
118
allPersons:async (root, args) => {
117
119
returnPerson.find({})
118
-
},
120
+
}
119
121
```
120
122
121
123
odottaa Apollo server promisen valmistumista ja lähettää promisen vastauksen kyselyn tekijälle. Apollo toimii siis suunnilleen seuraavasti:
122
124
123
125
```js
124
-
Person.find({}).then( result=> {
125
-
// palautetaan kyselyn tuloksena result
126
-
})
126
+
allPersons:async (root, args) => {
127
+
constresult=awaitPerson.find({})
128
+
return result
129
+
}
127
130
```
128
131
129
132
Täydennetään vielä resolveri _allPersons_ ottamaan huomioon optionaalinen filtterinä toimiva parametri _phone_:
GraphQL:n lisäksi syötteet validoidaan nyt Mongoose-skeemassa määriteltyjä validointeja käyttäen. Skeemassa olevien validointivirheiden varalta _save_-metodeille täytyy lisätä virheen käsittelevä _try/catch_-lohko. Heitetään catchiin jouduttaessa vastaukseksi sopiva poikkeus, joka on tällä kertaa [UserInputError](https://www.apollographql.com/docs/apollo-server/data/errors/):
161
+
GraphQL:n lisäksi syötteet validoidaan nyt Mongoose-skeemassa määriteltyjä validointeja käyttäen. Skeemassa olevien validointivirheiden varalta _save_-metodeille täytyy lisätä virheen käsittelevä _try/catch_-lohko. Heitetään catchiin jouduttaessa vastaukseksi [virhekoodilla](https://www.apollographql.com/docs/apollo-server/data/errors/#built-in-error-codes)*BAD\_USER\_INPUT* varustetu poikkeus [GraphQLError](https://www.apollographql.com/docs/apollo-server/data/errors/#custom-errors):
159
162
160
163
```js
161
164
Mutation: {
@@ -166,8 +169,12 @@ Mutation: {
166
169
try {
167
170
awaitperson.save()
168
171
} catch (error) {
169
-
thrownewUserInputError(error.message, {
170
-
invalidArgs: args,
172
+
thrownewGraphQLError('Saving person failed', {
173
+
extensions: {
174
+
code:'BAD_USER_INPUT',
175
+
invalidArgs:args.name,
176
+
error
177
+
}
171
178
})
172
179
}
173
180
// highlight-end
@@ -182,8 +189,12 @@ Mutation: {
182
189
try {
183
190
awaitperson.save()
184
191
} catch (error) {
185
-
thrownewUserInputError(error.message, {
186
-
invalidArgs: args,
192
+
thrownewGraphQLError('Saving number failed', {
193
+
extensions: {
194
+
code:'BAD_USER_INPUT',
195
+
invalidArgs:args.name,
196
+
error
197
+
}
187
198
})
188
199
}
189
200
// highlight-end
@@ -193,6 +204,8 @@ Mutation: {
193
204
}
194
205
```
195
206
207
+
Mongoosen virheen tiedot ja ongelman aiheuttanut data on nyt liitetty poikkeuksen konfiguraatio-olioon <i>extensions</i>, näin ne saadaan välitettyä kutsujalle.
208
+
196
209
Backendin koodi on kokonaisuudessaan [GitHubissa](https://github.com/fullstack-hy2020/graphql-phonebook-backend/tree/part8-4), branchissa <i>part8-4</i>.
0 commit comments