From a3242ecd996262031a0740e5d0256e9415b21404 Mon Sep 17 00:00:00 2001 From: Zino Hofmann Date: Fri, 21 Sep 2018 19:14:30 +0200 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=94=A7=20Fix=20connectivity=20errors?= =?UTF-8?q?=20not=20being=20thown=20on=20and=20streamed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/core/query_manager.dart | 64 +++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/lib/src/core/query_manager.dart b/lib/src/core/query_manager.dart index cc3dda283..023828ad2 100644 --- a/lib/src/core/query_manager.dart +++ b/lib/src/core/query_manager.dart @@ -76,6 +76,7 @@ class QueryManager { FetchResult fetchResult; QueryResult queryResult; + Exception exception; if (options.context != null) { operation.setContext(options.context); @@ -112,37 +113,46 @@ class QueryManager { } // execute the operation trough the provided link(s) - fetchResult = await execute( - link: link, - operation: operation, - ).first; - - // save the data from fetchResult to the cache - if (fetchResult.data != null && - options.fetchPolicy != FetchPolicy.noCache) { - cache.write( - operation.toKey(), - fetchResult.data, - ); - } + try { + fetchResult = await execute( + link: link, + operation: operation, + ).first; + + // save the data from fetchResult to the cache + if (fetchResult.data != null && + options.fetchPolicy != FetchPolicy.noCache) { + cache.write( + operation.toKey(), + fetchResult.data, + ); + } - if (fetchResult.data == null && - fetchResult.errors == null && - (options.fetchPolicy == FetchPolicy.noCache || - options.fetchPolicy == FetchPolicy.networkOnly)) { - throw Exception( - 'Could not resolve that operation on the network. (${options.fetchPolicy.toString()})', - ); - } + if (fetchResult.data == null && + fetchResult.errors == null && + (options.fetchPolicy == FetchPolicy.noCache || + options.fetchPolicy == FetchPolicy.networkOnly)) { + throw Exception( + 'Could not resolve that operation on the network. (${options.fetchPolicy.toString()})', + ); + } - queryResult = _mapFetchResultToQueryResult(fetchResult); + queryResult = _mapFetchResultToQueryResult(fetchResult); - // add the result to an observable query if it exists and not closed - if (observableQuery != null && !observableQuery.controller.isClosed) { - observableQuery.controller.add(queryResult); - } + // add the result to an observable query if it exists and not closed + if (observableQuery != null && !observableQuery.controller.isClosed) { + observableQuery.controller.add(queryResult); + } - return queryResult; + return queryResult; + } catch (error) { + // add the error to an observable query if it exists and not closed + if (observableQuery != null && !observableQuery.controller.isClosed) { + observableQuery.controller.addError(error); + } + + rethrow; + } } ObservableQuery getQuery(String queryId) { From 1247164c3ac8306c25d21f506751691fd9cd876b Mon Sep 17 00:00:00 2001 From: Zino Hofmann Date: Tue, 25 Sep 2018 20:11:29 +0200 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=97=91=20Remove=20unused=20variable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/core/query_manager.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/core/query_manager.dart b/lib/src/core/query_manager.dart index 023828ad2..475758f41 100644 --- a/lib/src/core/query_manager.dart +++ b/lib/src/core/query_manager.dart @@ -76,7 +76,6 @@ class QueryManager { FetchResult fetchResult; QueryResult queryResult; - Exception exception; if (options.context != null) { operation.setContext(options.context); From 4d7a0ca52c4095cae0ee565f2e95253187f3b978 Mon Sep 17 00:00:00 2001 From: Zino Hofmann Date: Tue, 25 Sep 2018 21:37:00 +0200 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=94=A7=20Add=20errors=20the=20the=20g?= =?UTF-8?q?raphql=20result=20rather=20then=20trowing=20an=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xcshareddata/WorkspaceSettings.xcsettings | 8 ++ lib/src/core/graphql_error.dart | 9 +- lib/src/core/query_manager.dart | 82 ++++++++++--------- lib/src/core/query_result.dart | 10 ++- lib/src/widgets/query.dart | 2 + 5 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings diff --git a/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 000000000..949b67898 --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + BuildSystemType + Original + + diff --git a/lib/src/core/graphql_error.dart b/lib/src/core/graphql_error.dart index be7f296cb..1af6d6218 100644 --- a/lib/src/core/graphql_error.dart +++ b/lib/src/core/graphql_error.dart @@ -29,6 +29,13 @@ class GraphQLError { /// Custom error data returned by your GraphQL API server final Map extensions; + GraphQLError({ + this.message, + this.locations, + this.path, + this.extensions, + }); + /// Constructs a [GraphQLError] from a JSON map. GraphQLError.fromJSON(dynamic data) : message = data['message'], @@ -44,5 +51,5 @@ class GraphQLError { @override String toString() => - '$message: ${locations is List ? locations.map((Location l) => '[${l.toString()}]').join('') : ""}'; + '$message: ${locations is List ? locations.map((Location l) => '[${l.toString()}]').join('') : "Undefind location"}'; } diff --git a/lib/src/core/query_manager.dart b/lib/src/core/query_manager.dart index 475758f41..33510f78c 100644 --- a/lib/src/core/query_manager.dart +++ b/lib/src/core/query_manager.dart @@ -77,42 +77,42 @@ class QueryManager { FetchResult fetchResult; QueryResult queryResult; - if (options.context != null) { - operation.setContext(options.context); - } + try { + if (options.context != null) { + operation.setContext(options.context); + } - if (options.fetchPolicy == FetchPolicy.cacheFirst || - options.fetchPolicy == FetchPolicy.cacheAndNetwork || - options.fetchPolicy == FetchPolicy.cacheOnly) { - final dynamic cachedData = cache.read(operation.toKey()); + if (options.fetchPolicy == FetchPolicy.cacheFirst || + options.fetchPolicy == FetchPolicy.cacheAndNetwork || + options.fetchPolicy == FetchPolicy.cacheOnly) { + final dynamic cachedData = cache.read(operation.toKey()); - if (cachedData != null) { - fetchResult = FetchResult( - data: cachedData, - ); + if (cachedData != null) { + fetchResult = FetchResult( + data: cachedData, + ); - queryResult = _mapFetchResultToQueryResult(fetchResult); + queryResult = _mapFetchResultToQueryResult(fetchResult); - // add the result to an observable query if it exists - if (observableQuery != null) { - observableQuery.controller.add(queryResult); - } + // add the result to an observable query if it exists + if (observableQuery != null) { + observableQuery.controller.add(queryResult); + } - if (options.fetchPolicy == FetchPolicy.cacheFirst || - options.fetchPolicy == FetchPolicy.cacheOnly) { - return queryResult; + if (options.fetchPolicy == FetchPolicy.cacheFirst || + options.fetchPolicy == FetchPolicy.cacheOnly) { + return queryResult; + } } - } - if (options.fetchPolicy == FetchPolicy.cacheOnly) { - throw Exception( - 'Could not find that operation in the cache. (${options.fetchPolicy.toString()})', - ); + if (options.fetchPolicy == FetchPolicy.cacheOnly) { + throw Exception( + 'Could not find that operation in the cache. (${options.fetchPolicy.toString()})', + ); + } } - } - // execute the operation trough the provided link(s) - try { + // execute the operation trough the provided link(s) fetchResult = await execute( link: link, operation: operation, @@ -137,21 +137,27 @@ class QueryManager { } queryResult = _mapFetchResultToQueryResult(fetchResult); - - // add the result to an observable query if it exists and not closed - if (observableQuery != null && !observableQuery.controller.isClosed) { - observableQuery.controller.add(queryResult); - } - - return queryResult; } catch (error) { - // add the error to an observable query if it exists and not closed - if (observableQuery != null && !observableQuery.controller.isClosed) { - observableQuery.controller.addError(error); + final GraphQLError graphQLError = GraphQLError( + message: error.message, + ); + + if (queryResult != null) { + queryResult.addError(graphQLError); + } else { + queryResult = QueryResult( + loading: false, + ); + queryResult.addError(graphQLError); } + } - rethrow; + // add the result to an observable query if it exists and not closed + if (observableQuery != null && !observableQuery.controller.isClosed) { + observableQuery.controller.add(queryResult); } + + return queryResult; } ObservableQuery getQuery(String queryId) { diff --git a/lib/src/core/query_result.dart b/lib/src/core/query_result.dart index 69b6916d7..07bc7ef18 100644 --- a/lib/src/core/query_result.dart +++ b/lib/src/core/query_result.dart @@ -19,6 +19,14 @@ class QueryResult { return false; } - return errors.isEmpty; + return errors.isNotEmpty; + } + + void addError(GraphQLError graphQLError) { + if (errors != null) { + errors.add(graphQLError); + } else { + errors = [graphQLError]; + } } } diff --git a/lib/src/widgets/query.dart b/lib/src/widgets/query.dart index 635e981a0..336d0bed2 100644 --- a/lib/src/widgets/query.dart +++ b/lib/src/widgets/query.dart @@ -42,6 +42,8 @@ class QueryState extends State { @override void didChangeDependencies() async { + print('didChangeDependencies'); + /// Gets the client from the closest wrapping [GraphQLProvider]. client = GraphQLProvider.of(context).value; assert(client != null); From 261b4ec6c60c3a9c715595bf936aa252efbcafa4 Mon Sep 17 00:00:00 2001 From: Zino Hofmann Date: Tue, 25 Sep 2018 22:18:29 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=97=91=20Remove=20debugging=20print?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/widgets/query.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/src/widgets/query.dart b/lib/src/widgets/query.dart index 336d0bed2..635e981a0 100644 --- a/lib/src/widgets/query.dart +++ b/lib/src/widgets/query.dart @@ -42,8 +42,6 @@ class QueryState extends State { @override void didChangeDependencies() async { - print('didChangeDependencies'); - /// Gets the client from the closest wrapping [GraphQLProvider]. client = GraphQLProvider.of(context).value; assert(client != null);