Skip to content

Commit

Permalink
Merge pull request #107 from zino-app/patch-14
Browse files Browse the repository at this point in the history
🔧 Fix connectivity errors not being thown on and streamed
  • Loading branch information
HofmannZ authored Sep 25, 2018
2 parents fa79985 + 261b4ec commit b26fe51
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildSystemType</key>
<string>Original</string>
</dict>
</plist>
9 changes: 8 additions & 1 deletion lib/src/core/graphql_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class GraphQLError {
/// Custom error data returned by your GraphQL API server
final Map<String, dynamic> extensions;

GraphQLError({
this.message,
this.locations,
this.path,
this.extensions,
});

/// Constructs a [GraphQLError] from a JSON map.
GraphQLError.fromJSON(dynamic data)
: message = data['message'],
Expand All @@ -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"}';
}
101 changes: 58 additions & 43 deletions lib/src/core/query_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,66 +77,81 @@ 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) {
// 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,
);
}

if (fetchResult.data == null &&
fetchResult.errors == null &&
(options.fetchPolicy == FetchPolicy.noCache ||
options.fetchPolicy == FetchPolicy.networkOnly)) {
throw Exception(
'Could not find that operation in the cache. (${options.fetchPolicy.toString()})',
'Could not resolve that operation on the network. (${options.fetchPolicy.toString()})',
);
}
}

// 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,
queryResult = _mapFetchResultToQueryResult(fetchResult);
} catch (error) {
final GraphQLError graphQLError = GraphQLError(
message: error.message,
);
}

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 (queryResult != null) {
queryResult.addError(graphQLError);
} else {
queryResult = QueryResult(
loading: false,
);
queryResult.addError(graphQLError);
}
}

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);
Expand Down
10 changes: 9 additions & 1 deletion lib/src/core/query_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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>[graphQLError];
}
}
}

0 comments on commit b26fe51

Please # to comment.