Handle errors

As we saw in the previous step, Apollo returned error and results object in completion handler.

Now let's go back to the todoQueryCloud function that you wrote in the previous step.

private func todoQueryCloud(){
apollo.fetch(query: GetMyTodosQuery()){ (result, error) in
if ((error) != nil) {
if SessionManager.shared.logout() {
self.performSegue(withIdentifier: "loginVC", sender: self)
}
return
}
guard let data = result?.data else { return }
self.todos = data.todos
self.filteredTodos = data.todos
DispatchQueue.main.async {
self.setupUI()
self.todoTable.reloadData()
}
}
}

When this function is executed in the viewWillAppear, we handle the completion of it from the error or result objects.

Error state in Apollo iOS

Now, the query could also end up in an error state due to various reasons. Sometimes the graphql query could be wrong, or the server isn't responding. Whatever may be the reason, the user facing UI should show something to convey that an error has occurred. Here we are checking if there was a error, was that because of an invalid token? And if so, we trigger logout and send the user to login screen. In this error state, typically you can send these error messages to third-party services to track what went wrong.

All said and done, these are two important states that need to be handled inside your component. What you have written above is basic, but sufficient for this tutorial.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin