fix: Fetch more is broken with `parserFn`
MODIFY
packages/graphql/lib/src/core/fetch_more.dart
packages/graphql/lib/src/core/fetch_more.dart
@@ -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);

MODIFY
packages/graphql/lib/src/core/query_options.dart
packages/graphql/lib/src/core/query_options.dart
+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';
@@ -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,
@@ -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(

MODIFY
packages/graphql/test/graphql_client_test.dart
packages/graphql/test/graphql_client_test.dart
@@ -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'''

fix: Fetch more is broken with `parserFn`