From 3b2e10ee433803cad2923c6d5cac7567b3c6b70f Mon Sep 17 00:00:00 2001 From: machard Date: Sun, 27 Nov 2016 10:06:33 -0300 Subject: [PATCH] fix rendering --- src/graphql.tsx | 55 ++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/graphql.tsx b/src/graphql.tsx index c875c381eb..62c5d7f7e1 100644 --- a/src/graphql.tsx +++ b/src/graphql.tsx @@ -282,7 +282,10 @@ export default function graphql( this.subscribeToQuery(nextProps); if (this.shouldSkip(this.props) && !this.shouldSkip(nextProps)) { - this.queryObservable.refetch(); + this.queryObservable.refetch().then( + (r) => this.next(r), + (e) => this.handleError(e) + ); } } @@ -333,6 +336,29 @@ export default function graphql( } } + next(results: any) { + if (this.type === DocumentType.Subscription) { + // Subscriptions don't currently support `currentResult`, so we + // need to do this ourselves + this.lastSubscriptionData = results; + + results = { data: results }; + } + const clashingKeys = Object.keys(observableQueryFields(results.data)); + invariant(clashingKeys.length === 0, + `the result of the '${graphQLDisplayName}' operation contains keys that ` + + `conflict with the return object.` + + clashingKeys.map(k => `'${k}'`).join(', ') + ` not allowed.` + ); + + this.forceRenderChildren(); + }; + + handleError(error) { + if (error instanceof ApolloError) return next({ error }); + throw error; + }; + subscribeToQuery(props): boolean { const opts = calculateOptions(props) as QueryOptions; @@ -356,28 +382,6 @@ export default function graphql( this.createQuery(opts); } - const next = (results: any) => { - if (this.type === DocumentType.Subscription) { - // Subscriptions don't currently support `currentResult`, so we - // need to do this ourselves - this.lastSubscriptionData = results; - - results = { data: results }; - } - const clashingKeys = Object.keys(observableQueryFields(results.data)); - invariant(clashingKeys.length === 0, - `the result of the '${graphQLDisplayName}' operation contains keys that ` + - `conflict with the return object.` + - clashingKeys.map(k => `'${k}'`).join(', ') + ` not allowed.` - ); - - this.forceRenderChildren(); - }; - - const handleError = (error) => { - if (error instanceof ApolloError) return next({ error }); - throw error; - }; /* Since `setState()` can throw an error if the child had a render error, @@ -386,7 +390,10 @@ export default function graphql( Instead, we subscribe to the store for network errors and re-render that way */ - this.querySubscription = this.queryObservable.subscribe({ next, error: handleError }); + this.querySubscription = this.queryObservable.subscribe({ + next: (r) => this.next(r), + error: (e) => this.handleError(e) + }); } unsubscribeFromQuery() {