menu

Apollo

A community of developers, designers and others who love Apollo and GraphQL. 🚀

Channels
# All channels
view-forward
# General
view-forward
# Apollo Angular
view-forward
# Announcements
view-forward
# Apollo Android
view-forward
# Apollo Client
view-forward
# Apollo iOS
view-forward
# Apollo Link
view-forward
# Apollo Link Rest
view-forward
# Local State
view-forward
# Apollo Studio
view-forward
# Apollo Server
view-forward
# Apollo Tooling
view-forward
# Contributing
view-forward
# Docs
view-forward
# Events
view-forward
# GraphQL Tools
view-forward
# Jobs
view-forward
# Random
view-forward
# React Apollo
view-forward
# Showcase
view-forward
# Subscriptions
view-forward
# Testing
view-forward
# Vue Apollo
view-forward
Team

Apollo error link - setState inside onError method

October 7, 2020 at 11:25am

Apollo error link - setState inside onError method

October 7, 2020 at 11:25am (Edited 6 months ago)
Hi, I'm trying to display the error message I'm getting from the apollo API call to the user, so I try to save my current error in the state of my ApolloProvider inside the onError method. The problem is the onError method doesn't stop to rerun and then my page is rerendering again and again.
My goal: when I ave an API error to save the message error in my state then if the errorMsg is not empty to display the message to the user and after few seconds to reset the error message to '' and then te m message will disappear.
const resetError => setError('')
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors && graphQLErrors?.length > 0) {
setError(graphQLErrors[0].message)
graphQLErrors.forEach(({ message, locations, path }) =>
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
)
}
if (networkError) console.log(`[Network error]: ${networkError}`)
})
return (
<ApolloProvider client={apolloClient}>
{children}
{error && <Toast message={getMessage(100)} resetError={resetError}/>}
</ApolloProvider>
)
Thanks!
No messages yet