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/part8b.md
+8-26Lines changed: 8 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -896,46 +896,28 @@ the mutation response is <i>null</i>:
896
896
897
897

898
898
899
-
For GraphQL, this is not an error, so registering an *onError* error handler is not useful.
900
-
901
-
We can use the *result* field returned by the *useMutation* hook as its second parameter to generate an error message.
899
+
Since this isn’t considered an error state from GraphQL’s point of view, registering an _onError_ error handler wouldn’t be useful in this situation. However, we can add an _onCompleted_ callback to the _useMutation_ hook, where we can generate a potential error message:
const [ changeNumber, result ] =useMutation(EDIT_NUMBER) // highlight-line
913
-
914
-
constsubmit= (event) => {
915
-
event.preventDefault()
916
-
917
-
changeNumber({ variables: { name, phone } })
918
-
919
-
setName('')
920
-
setPhone('')
921
-
}
922
-
923
906
// highlight-start
924
-
useEffect(() => {
925
-
if (result.data&&result.data.editNumber===null) {
926
-
setError('person not found')
907
+
const [changeNumber] =useMutation(EDIT_NUMBER, {
908
+
onCompleted: (data) => {
909
+
if (!data.editNumber) {
910
+
setError('person not found')
911
+
}
927
912
}
928
-
929
-
}, [result.data, setError])
913
+
})
930
914
// highlight-end
931
915
932
916
// ...
933
917
}
934
918
```
935
919
936
-
If a person cannot be found, or the *result.data.editNumber* is *null*, the component uses the callback function it received as props to set a suitable error message.
937
-
We want to set the error message only when the result of the mutation
938
-
*result.data* changes, so we use the useEffect hook to control setting the error message.
920
+
The _onCompleted_ callback function is always executed when the mutation has been successfully completed. If the person wasn’t found—that is, if the query result _data.editNumber_ is _null_—the component uses the _setError_ callback function it received via props to set an appropriate error message.
939
921
940
922
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/osa8b.md
+8-24Lines changed: 8 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -880,44 +880,28 @@ Sovelluksessa on vielä pieni ongelma. Jos yritämme vaihtaa olemattomaan nimee
880
880
881
881

882
882
883
-
Koska kyseessä ei ole GraphQL:n kannalta virhetilanne, ei _onError_-virheenkäsittelijän rekisteröimisestä olisi tässä tilanteessa hyötyä.
884
-
885
-
Voimme generoida virheilmoituksen _useMutation_-hookin toisena parametrina palauttaman mutaation tuloksen kertovan olion _result_ avulla.
883
+
Koska kyseessä ei ole GraphQL:n kannalta virhetilanne, ei _onError_-virheenkäsittelijän rekisteröimisestä olisi tässä tilanteessa hyötyä. Voimme kuitenkin lisätä _useMutation_-hookille _onCompleted_-takaisinkutsufunktion, jossa mahdollinen virheilmoitus voidaan generoida:
886
884
887
885
```js
888
-
import { useEffect, useState } from 'react' // highlight-line
889
-
import { useMutation } from '@apollo/client/react'
const [ changeNumber, result ] = useMutation(EDIT_NUMBER) // highlight-line
897
-
898
-
const submit = (event) => {
899
-
event.preventDefault()
900
-
901
-
changeNumber({ variables: { name, phone } })
902
-
903
-
setName('')
904
-
setPhone('')
905
-
}
906
-
907
890
// highlight-start
908
-
useEffect(() => {
909
-
if (result.data && result.data.editNumber === null) {
910
-
setError('person not found')
891
+
const [changeNumber] = useMutation(EDIT_NUMBER, {
892
+
onCompleted: (data) => {
893
+
if (!data.editNumber) {
894
+
setError('person not found')
895
+
}
911
896
}
912
-
913
-
}, [result.data, setError])
897
+
})
914
898
// highlight-end
915
899
916
900
// ...
917
901
}
918
902
```
919
903
920
-
Jos henkilöä ei löytynyt, eli kyselyn tulos _result.data.editNumber_ on _null_, asettaa komponentti propseina saamansa callback-funktion avulla sopivan virheilmoituksen. Virheilmoituksen asettamista kontrolloidaan useEffect-hookin avulla, ja hook suoritetaan aina kun mutaation tulos _result.data_ muuttuu.
904
+
Takaisinkutsufunktio _onCompleted_ suoritetaan aina, kun mutaatio on onnistuneesti suoritettu. Jos henkilöä ei löytynyt, eli kyselyn tulos _data.editNumber_ on _null_, asettaa komponentti propseina saamansa callback-funktion _setError_ avulla sopivan virheilmoituksen.
921
905
922
906
Sovelluksen tämänhetkinen koodi on [GitHubissa](https://github.com/fullstack-hy2020/graphql-phonebook-frontend/tree/part8-4), branchissa <i>part8-4</i>.
0 commit comments