Skip to content

Commit

Permalink
Merge pull request zino-hofmann#1044 from zino-hofmann/fix-fetch-more
Browse files Browse the repository at this point in the history
fix: Fetch more is broken with `parserFn`
  • Loading branch information
vincenzopalazzo authored Feb 17, 2022
2 parents 2306e12 + ed64e69 commit 14f1a21
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
12 changes: 2 additions & 10 deletions packages/graphql/lib/src/core/fetch_more.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@ Future<QueryResult<TParsed>> fetchMoreImplementation<TParsed>(
}) async {
// fetch more and update

final document = (fetchMoreOptions.document ?? originalOptions.document);
final request = originalOptions.asRequest;

final combinedOptions = QueryOptions<TParsed>(
fetchPolicy: FetchPolicy.noCache,
errorPolicy: originalOptions.errorPolicy,
document: document,
variables: {
...originalOptions.variables,
...fetchMoreOptions.variables,
},
);
final combinedOptions =
originalOptions.withFetchMoreOptions(fetchMoreOptions);

QueryResult<TParsed> fetchMoreResult =
await queryManager.query(combinedOptions);
Expand Down
19 changes: 18 additions & 1 deletion packages/graphql/lib/src/core/query_options.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:gql/language.dart';
import 'package:graphql/src/core/_base_options.dart';
import 'package:graphql/src/core/result_parser.dart';
import 'package:graphql/src/utilities/helpers.dart';
Expand Down Expand Up @@ -42,6 +43,22 @@ class QueryOptions<TParsed> extends BaseOptions<TParsed> {
pollInterval,
];

QueryOptions<TParsed> withFetchMoreOptions(
FetchMoreOptions fetchMoreOptions,
) =>
QueryOptions<TParsed>(
document: fetchMoreOptions.document ?? document,
operationName: operationName,
fetchPolicy: FetchPolicy.noCache,
errorPolicy: errorPolicy,
parserFn: parserFn,
context: context,
variables: {
...variables,
...fetchMoreOptions.variables,
},
);

WatchQueryOptions<TParsed> asWatchQueryOptions({bool fetchResults = true}) =>
WatchQueryOptions(
document: document,
Expand All @@ -54,7 +71,7 @@ class QueryOptions<TParsed> extends BaseOptions<TParsed> {
fetchResults: fetchResults,
context: context,
optimisticResult: optimisticResult,
parserFn: this.parserFn,
parserFn: parserFn,
);

QueryOptions<TParsed> copyWithPolicies(Policies policies) => QueryOptions(
Expand Down
70 changes: 70 additions & 0 deletions packages/graphql/test/graphql_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,76 @@ void main() {
equals('bar'),
);
});
test('successful fetch-more with parser', () async {
final ResultParserFn<List<String>> parserFn = (data) {
return data['viewer']['repositories']['nodes']
.map<String>((node) => node['name'] as String)
.toList();
};
final _options = QueryOptions(
document: parseString(readRepositories),
variables: <String, dynamic>{
'nRepositories': 42,
},
parserFn: parserFn,
);
final repoData = readRepositoryData(withTypenames: true);

when(
link.request(any),
).thenAnswer(
(_) => Stream.fromIterable([
Response(
data: repoData,
context: Context().withEntry(
HttpLinkResponseContext(
statusCode: 200,
headers: {'foo': 'bar'},
),
),
),
]),
);

final QueryResult<List<String>> r1 = await client.query(_options);
expect(r1.exception, isNull);
expect(r1.data, equals(repoData));
expect(
r1.parsedData,
equals([
'pq',
'go-evercookie',
'watchbot',
]));
final QueryResult<List<String>> r2 = await client.fetchMore(
FetchMoreOptions(
updateQuery: (d1, d2) => ({
'viewer': {
'repositories': {
'nodes': [
...d1!['viewer']['repositories']['nodes'],
...d2!['viewer']['repositories']['nodes'],
]
}
}
}),
),
previousResult: r1,
originalOptions: _options,
);

expect(r2.exception, isNull);
expect(
r2.parsedData,
equals([
'pq',
'go-evercookie',
'watchbot',
'pq',
'go-evercookie',
'watchbot',
]));
});

test('successful response without normalization', () async {
final readUnidentifiedRepositories = parseString(r'''
Expand Down

0 comments on commit 14f1a21

Please sign in to comment.