Kevin Danikowski
Kevin Danikowski

Reputation: 5206

How to Catch Errors from Apollo/GraphQL

I use graphCool as a backend, but there is a bug which won't be fixed any time soon.

Making a delete mutation on File schema will always throw this error (not a big deal):

"GraphQL error: Whoops. Looks like an internal server error. Please contact us from the Console (https://console.graph.cool) or via email ([email protected]) and include your Request ID:"

The call works, but I need to access the update function after the call and I can't because of the error.

Two solutions: 1. Catch the graphql error so I can reach update: function, how can I do this? 2. Update the store without using the update: function, how can I do this?

Here is my code:

_deleteImageFile = async () => {
    // THIS WORKS, BUT GET AN ERROR
    const socialPostId = this.props.socialPost.id
    // Tried to wrap the following in try catch without success
    await this.props.deleteImageFileMutation({
        variables: {
            id: this.props.socialPost.image.id
        }, //Error hits here "internal service error..."
        update: (store) => { //This is never run because of error
            console.log('this text will never log')
            const userId = localStorage.getItem(GC_USER_ID)
            const data = store.readQuery({query: ALL_SOCIAL_POSTS_QUERY, 
                variables: {
                    id: userId
            }})
            // Need to change the store here
            store.writeQuery({query: ALL_SOCIAL_POSTS_QUERY, data, 
                variables: {
                    id: userId
            }}).catch(res => { const errors = res.graphQLErrors; console.log(errors)})
            //The catch was an attempt to fix it
        }
    })
}

Reference Bug: https://github.com/graphcool/framework/issues/434

Upvotes: 1

Views: 1727

Answers (1)

Ionut Achim
Ionut Achim

Reputation: 977

You could try to pass down another dummy mutation and do that after the deleteImageFileMutation throws.

try {
  await this.props.deleteImageFileMutation ...
} catch (e) {
  // ...
} finally {
  await this.props.dummyMutation ...
}

Upvotes: 2

Related Questions