apollographql / apollo-link

:link: Interface for fetching and modifying control flow of GraphQL requests
https://www.apollographql.com/docs/link/
MIT License
1.44k stars 344 forks source link

OnError Apollo i will like to show/display the error #1281

Open unutoiul opened 4 years ago

unutoiul commented 4 years ago
const errorLink: any = onError(
    ({ operation, forward, graphQLErrors, networkError }) => {
       const { dispatchModal } = useContext<IModalContext>(ModalContext);
        console.log(operation);
        console.log(forward);

        if (graphQLErrors) {
            graphQLErrors.forEach(({ message, locations, path }) =>
                console.log(
                    `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
                )
            );
        }

        if (networkError) {
            console.log(`[Network error]: ${networkError}`);
        }
    }
);

in the code above I will like to get set the useContext of another react component but I get

Error: Invalid hook call. Hooks can only be called inside of the body of a function component

which is normal, but how can I actually display an error in a component or change the state of another component from onError function with apollo?

cmnstmntmn commented 4 years ago

@unutoiul did you found an workaround for this issue?

unutoiul commented 4 years ago

I have not, Did u find any?

cmnstmntmn commented 4 years ago

a silly hack

    return new Observable((observer) => {
      return observer.error([...graphQLErrors, networkError]);
    });