Skip to content

Commit

Permalink
Merge pull request zino-hofmann#394 from micimize/fetchmore_patch
Browse files Browse the repository at this point in the history
Fetchmore, json encoding, and query var change patches
  • Loading branch information
micimize authored Aug 29, 2019
2 parents 443d5b5 + af89b19 commit 883b072
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
27 changes: 21 additions & 6 deletions packages/graphql/lib/src/core/observable_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ class ObservableQuery {
void onListen() {
if (_latestWasEagerlyFetched) {
_latestWasEagerlyFetched = false;

// eager results are resolved synchronously,
// so we have to add them manually now that
// the stream is available
if (!controller.isClosed && latestResult != null) {
controller.add(latestResult);
}
return;
}
if (options.fetchResults) {
Expand Down Expand Up @@ -136,7 +143,7 @@ class ObservableQuery {
assert(fetchMoreOptions.updateQuery != null);

final combinedOptions = QueryOptions(
fetchPolicy: FetchPolicy.networkOnly,
fetchPolicy: FetchPolicy.noCache,
errorPolicy: options.errorPolicy,
document: fetchMoreOptions.document ?? options.document,
context: options.context,
Expand All @@ -155,11 +162,18 @@ class ObservableQuery {
QueryResult fetchMoreResult = await queryManager.query(combinedOptions);

try {
// combine the query with the new query, using the function provided by the user
fetchMoreResult.data = fetchMoreOptions.updateQuery(
latestResult.data,
fetchMoreResult.data,
);
assert(fetchMoreResult.data != null, 'updateQuery result cannot be null');
// stream the new results and rebuild
queryManager.addQueryResult(
queryId,
fetchMoreResult,
writeToCache: true,
);
} catch (error) {
if (fetchMoreResult.hasErrors) {
// because the updateQuery failure might have been because of these errors,
Expand All @@ -168,16 +182,17 @@ class ObservableQuery {
...(latestResult.errors ?? const []),
...fetchMoreResult.errors
];
addResult(latestResult);
queryManager.addQueryResult(
queryId,
latestResult,
writeToCache: true,
);

return;
} else {
rethrow;
}
}

// combine the query with the new query, using the fucntion provided by the user
// stream the new results and rebuild
addResult(fetchMoreResult);
}

/// add a result to the stream,
Expand Down
13 changes: 12 additions & 1 deletion packages/graphql/lib/src/core/query_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,19 @@ class QueryManager {
}

/// Add a result to the query specified by `queryId`, if it exists
void addQueryResult(String queryId, QueryResult queryResult) {
void addQueryResult(
String queryId,
QueryResult queryResult, {
bool writeToCache = false,
}) {
final ObservableQuery observableQuery = getQuery(queryId);
if (writeToCache) {
cache.write(
observableQuery.options.toKey(),
queryResult.data,
);
}

if (observableQuery != null && !observableQuery.controller.isClosed) {
observableQuery.addResult(queryResult);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql/lib/src/core/raw_operation_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class RawOperationData {
if (isIoFile(object)) {
return object.path;
}
return object;
// default toEncodable behavior
return object.toJson();
});

return '$document|$encodedVariables|$_identifier';
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ FetchMoreOptions opts = FetchMoreOptions(
];
// to avoid a lot of work, lets just update the list of repos in returned
// data with new data, this also ensure we have the endCursor already set
// data with new data, this also ensures we have the endCursor already set
// correctly
fetchMoreResultData['search']['nodes'] = repos;
Expand Down
1 change: 1 addition & 0 deletions packages/graphql_flutter/lib/src/widgets/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class QueryState extends State<Query> {
@override
Widget build(BuildContext context) {
return StreamBuilder<QueryResult>(
key: Key(observableQuery?.options?.toKey()),
initialData: observableQuery?.latestResult ?? QueryResult(loading: true),
stream: observableQuery.stream,
builder: (
Expand Down

0 comments on commit 883b072

Please sign in to comment.