From 47a1c92fa95009952f4c89adad496a51ea6b4483 Mon Sep 17 00:00:00 2001 From: Hamed Rezaee <57184669+hamed-rezaee@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:07:52 +0100 Subject: [PATCH] feat(graphql_common): add queryRequestTimeout parameter to GraphQLClient class --- packages/graphql/lib/src/graphql_client.dart | 26 +++-- .../graphql/test/graphql_client_test.dart | 100 ++++++++++++++++++ 2 files changed, 117 insertions(+), 9 deletions(-) diff --git a/packages/graphql/lib/src/graphql_client.dart b/packages/graphql/lib/src/graphql_client.dart index 6d7a1edfd..25c2e1a73 100644 --- a/packages/graphql/lib/src/graphql_client.dart +++ b/packages/graphql/lib/src/graphql_client.dart @@ -51,16 +51,24 @@ class GraphQLClient implements GraphQLDataProxy { late final QueryManager queryManager; /// Create a copy of the client with the provided information. - GraphQLClient copyWith( - {Link? link, - GraphQLCache? cache, - DefaultPolicies? defaultPolicies, - bool? alwaysRebroadcast}) { + GraphQLClient copyWith({ + Link? link, + GraphQLCache? cache, + DefaultPolicies? defaultPolicies, + bool? alwaysRebroadcast, + DeepEqualsFn? deepEquals, + bool deduplicatePollers = false, + Duration? queryRequestTimeout, + }) { return GraphQLClient( - link: link ?? this.link, - cache: cache ?? this.cache, - defaultPolicies: defaultPolicies ?? this.defaultPolicies, - alwaysRebroadcast: alwaysRebroadcast ?? queryManager.alwaysRebroadcast); + link: link ?? this.link, + cache: cache ?? this.cache, + defaultPolicies: defaultPolicies ?? this.defaultPolicies, + alwaysRebroadcast: alwaysRebroadcast ?? queryManager.alwaysRebroadcast, + deepEquals: deepEquals, + deduplicatePollers: deduplicatePollers, + queryRequestTimeout: queryRequestTimeout ?? queryManager.requestTimeout, + ); } /// This registers a query in the [QueryManager] and returns an [ObservableQuery] diff --git a/packages/graphql/test/graphql_client_test.dart b/packages/graphql/test/graphql_client_test.dart index f9401f9eb..5fc283db4 100644 --- a/packages/graphql/test/graphql_client_test.dart +++ b/packages/graphql/test/graphql_client_test.dart @@ -1447,5 +1447,105 @@ query WalletGetContent($input: WalletGetContentInput!) { equals('bar'), ); }); + + group('GraphQLClient copyWith', () { + late GraphQLClient client; + late Link link; + late GraphQLCache cache; + late DefaultPolicies defaultPolicies; + late Duration queryRequestTimeout; + + setUp(() { + link = MockLink(); + cache = GraphQLCache(); + defaultPolicies = DefaultPolicies(); + queryRequestTimeout = const Duration(seconds: 5); + client = GraphQLClient( + link: link, + cache: cache, + defaultPolicies: defaultPolicies, + queryRequestTimeout: queryRequestTimeout, + ); + }); + + test('copyWith updates link', () { + final newLink = MockLink(); + final newClient = client.copyWith(link: newLink); + + expect(newClient.link, equals(newLink)); + expect(newClient.cache, equals(client.cache)); + expect(newClient.defaultPolicies, equals(client.defaultPolicies)); + expect( + newClient.queryManager.requestTimeout, + equals(client.queryManager.requestTimeout), + ); + }); + + test('copyWith updates cache', () { + final newCache = GraphQLCache(); + final newClient = client.copyWith(cache: newCache); + + expect(newClient.cache, equals(newCache)); + expect(newClient.link, equals(client.link)); + expect(newClient.defaultPolicies, equals(client.defaultPolicies)); + expect( + newClient.queryManager.requestTimeout, + equals(client.queryManager.requestTimeout), + ); + }); + + test('copyWith updates defaultPolicies', () { + final newDefaultPolicies = DefaultPolicies(); + final newClient = client.copyWith(defaultPolicies: newDefaultPolicies); + + expect(newClient.defaultPolicies, equals(newDefaultPolicies)); + expect(newClient.link, equals(client.link)); + expect(newClient.cache, equals(client.cache)); + expect( + newClient.queryManager.requestTimeout, + equals(client.queryManager.requestTimeout), + ); + }); + + test('copyWith updates alwaysRebroadcast', () { + final newClient = client.copyWith(alwaysRebroadcast: true); + + expect(newClient.queryManager.alwaysRebroadcast, isTrue); + expect(newClient.link, equals(client.link)); + expect(newClient.cache, equals(client.cache)); + expect(newClient.defaultPolicies, equals(client.defaultPolicies)); + expect( + newClient.queryManager.requestTimeout, + equals(client.queryManager.requestTimeout), + ); + }); + + test('copyWith updates queryRequestTimeout', () { + final newTimeout = const Duration(seconds: 10); + final newClient = client.copyWith(queryRequestTimeout: newTimeout); + + expect(newClient.queryManager.requestTimeout, equals(newTimeout)); + expect(newClient.link, equals(client.link)); + expect(newClient.cache, equals(client.cache)); + expect(newClient.defaultPolicies, equals(client.defaultPolicies)); + }); + + test('copyWith does not override any properties if none are provided', + () { + final newClient = client.copyWith(); + + expect(newClient.link, equals(client.link)); + expect(newClient.cache, equals(client.cache)); + expect(newClient.defaultPolicies, equals(client.defaultPolicies)); + expect( + newClient.queryManager.alwaysRebroadcast, + equals(client.queryManager.alwaysRebroadcast), + ); + expect( + newClient.queryManager.requestTimeout, + equals(client.queryManager.requestTimeout), + ); + }); + }); }); }