Giter Club home page Giter Club logo

Comments (9)

phryneas avatar phryneas commented on May 26, 2024

If this method would throw errors, none of our users could just call executeQuery() without additional error handling - they would get "uncaught Promise rejection" errors on the console.

You can check if the result contains an error property and act accordingly based on that.

That said, the useQuery hook is generally meant to synchronize with a React component, not with iterable code. Usually, you should be handling errors inside of your component's render function.

If you are looking for a way of making a query without having the results reflected in a component, you can call const client = useApolloClient() followed by client.query(...) to make a query directly without involving the React component cycle in this.

from apollo-client.

quocluongha avatar quocluongha commented on May 26, 2024

Thank you @phryneas for the clarification. How about other functions like refetch and fetchMore? Do I also need to check the error property instead of using catch block?

from apollo-client.

phryneas avatar phryneas commented on May 26, 2024

I think so, although the base advice for those is the same: If you're in a component, error handling should in most cases be done by the component itself, not by imperative handler code.

from apollo-client.

quocluongha avatar quocluongha commented on May 26, 2024

What do you mean by

error handling should in most cases be done by the component itself, not by imperative handler code.

In my case, I was implementing a button to execute the query whenever the button is pressed. And I am going to show an Alert when there is error with the query.

Before:
// Call this function when button is pressed
const handleGetData = async () => {
    try {
      const {data} = await getData();
      console.log('Success', data);
    } catch (error: any) {
      console.log('Error', error);
      Alert.alert('Error', error?.message);
    }
  };

After:
// Call this function when button is pressed
const handleGetData = async () => {
  const {data, error} = await getData();
      
  if (error) {
    Alert.alert('Error', error?.message);
    return;
  }
};

from apollo-client.

phryneas avatar phryneas commented on May 26, 2024

By that I mean that you could also have an Alert component that displays every time the error changes.

function Alert(error) {
  const [lastAcceptedError, acceptError] = useState(undefined)
  // either there has not been an error, or the user already acted on it, display nothing
  if (lastAcceptedError || lastAcceptedError == error) return null;

  return <div>these was an error <button onClick={() => acceptError(error)}>ok</button></div>
}

and then you use that in your component:

function MyComponent(){
  const result = useLazyQuery()

  return <>
    <Alert error={result.error} />
  // other normal content
  </>
}

There is usually no need to have any imperative code like this.

from apollo-client.

quocluongha avatar quocluongha commented on May 26, 2024

Oh thank you @phryneas.

Actually my Alert component is pretty complex, it allow me to custom the type of alert being shown (e.g the Alert background color will be yellow if type is warning). Because of that I decide to mount the Alert at the root of my project and utilize React's ref in children component to show or hide it.

So base on your advice then I should use React's useEffect to listen to the error and perform the ref call, right?

const {error} = useLazyQuery()

useEffect(() => {
  Alert.show({type: 'warning', message: error?.message})
}, [error])

from apollo-client.

phryneas avatar phryneas commented on May 26, 2024

If there's no way around that, yes, that's a valid way of doing so.

from apollo-client.

quocluongha avatar quocluongha commented on May 26, 2024

Thank you @phryneas. I'm closing this issue now.

from apollo-client.

github-actions avatar github-actions commented on May 26, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

from apollo-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.