Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphql_common): Add deduplicatePollers, deepEquals, and queryRequestTimeout parameters to copyWith method in GraphQLClient #1464

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions packages/graphql/lib/src/graphql_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
100 changes: 100 additions & 0 deletions packages/graphql/test/graphql_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
});
});
});
}
Loading