fix(smart query): handling errorPolicy, closes #526

This commit is contained in:
Guillaume Chau
2019-10-20 16:18:36 +02:00
parent ef9c7a0af0
commit ae4713e32a
+21 -2
View File
@@ -141,9 +141,9 @@ export default class SmartQuery extends SmartApollo {
nextResult (result) {
super.nextResult(result)
const { data, loading, error } = result
const { data, loading, error, errors } = result
if (error) {
if (error || errors) {
this.firstRunReject()
}
@@ -151,6 +151,25 @@ export default class SmartQuery extends SmartApollo {
this.loadingDone()
}
// If `errorPolicy` is set to `all`, an error won't be thrown
// Instead result will have an `errors` array of GraphQL Errors
// so we need to reconstruct an error object similar to the normal one
if (errors) {
const e = new Error(`GraphQL error: ${errors.map(e => e.message).join(' | ')}`)
Object.assign(e, {
graphQLErrors: errors,
networkError: null,
})
// We skip query catchError logic
// as we only want to dispatch the error
super.catchError(e)
}
if (this.observer.options.errorPolicy === 'none' && (error || errors)) {
// Don't apply result
return
}
const hasResultCallback = typeof this.options.result === 'function'
if (data == null) {