From ae4713e32a81f0c3fb77586168ed26d4e0aaa3a8 Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Sun, 20 Oct 2019 16:18:36 +0200 Subject: [PATCH] fix(smart query): handling errorPolicy, closes #526 --- src/smart-query.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/smart-query.js b/src/smart-query.js index 28329f0..6d360ac 100644 --- a/src/smart-query.js +++ b/src/smart-query.js @@ -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) {