如何从Apollo / GraphQL中捕获错误

时间:2017-12-01 17:33:31

标签: error-handling graphql react-apollo graphcool

我使用graphCool作为后端,但有一个错误,不会很快修复。

delete架构上制作mutation File将始终抛出此错误(不是什么大问题):

“GraphQL错误:糟糕。看起来像是内部服务器错误。请通过控制台(https://console.graph.cool)或通过电子邮件(support@graph.cool)与我们联系并提供您的请求ID:”

通话有效,但我需要在通话后访问update功能,因为错误我无法访问。

两个解决方案: 1.捕获graphql错误,以便我可以达到update:功能,我该怎么做? 2.不使用update:功能更新商店,我该怎么做?

这是我的代码:

_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
        }
    })
}

参考错误:https://github.com/graphcool/framework/issues/434

1 个答案:

答案 0 :(得分:2)

您可以尝试传递另一个虚拟变异,并在deleteImageFileMutation抛出后执行此操作。

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