Skip to content

Commit ebc96e7

Browse files
Merge pull request #1081 from zino-hofmann/vincenzopalazzo/update_client
feat: Add possibility to update the client components
2 parents 09cc1cc + 1c37a09 commit ebc96e7

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

packages/graphql/lib/src/graphql_client.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ class GraphQLClient implements GraphQLDataProxy {
4444

4545
late final QueryManager queryManager;
4646

47+
/// Create a copy of the client with the provided information.
48+
GraphQLClient copyWith(
49+
{Link? link,
50+
GraphQLCache? cache,
51+
DefaultPolicies? defaultPolicies,
52+
bool? alwaysRebroadcast}) {
53+
return GraphQLClient(
54+
link: link ?? this.link,
55+
cache: cache ?? this.cache,
56+
defaultPolicies: defaultPolicies ?? this.defaultPolicies,
57+
alwaysRebroadcast: alwaysRebroadcast ?? queryManager.alwaysRebroadcast);
58+
}
59+
4760
/// This registers a query in the [QueryManager] and returns an [ObservableQuery]
4861
/// based on the provided [WatchQueryOptions].
4962
///

packages/graphql/test/graphql_client_test.dart

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ void main() {
154154
equals('bar'),
155155
);
156156
});
157+
157158
test('successful response with parser', () async {
158159
final ResultParserFn<List<String>> parserFn = (data) {
159160
return (data['viewer']['repositories']['nodes'] as List)
@@ -1048,4 +1049,76 @@ void main() {
10481049
expect(client.readQuery(queryRequest), equals(updatedQueryData));
10491050
});
10501051
});
1052+
1053+
group("Client management", () {
1054+
setUp(() {
1055+
link = MockLink();
1056+
1057+
client = GraphQLClient(
1058+
cache: getTestCache(),
1059+
link: link,
1060+
);
1061+
});
1062+
1063+
test('successful response with update link', () async {
1064+
final _options = QueryOptions(
1065+
document: parseString(readRepositories),
1066+
variables: <String, dynamic>{
1067+
'nRepositories': 42,
1068+
},
1069+
);
1070+
final repoData = readRepositoryData(withTypenames: true);
1071+
1072+
when(
1073+
link.request(any),
1074+
).thenAnswer(
1075+
(_) => Stream.fromIterable([
1076+
Response(
1077+
data: repoData,
1078+
context: Context().withEntry(
1079+
HttpLinkResponseContext(
1080+
statusCode: 200,
1081+
headers: {'foo': 'bar'},
1082+
),
1083+
),
1084+
response: {},
1085+
),
1086+
]),
1087+
);
1088+
// FIXME(vincenzopalazzo): adding a new mock link, maybe to print some addition
1089+
// information.
1090+
link = MockLink();
1091+
when(
1092+
link.request(any),
1093+
).thenAnswer(
1094+
(_) => Stream.fromIterable([
1095+
Response(
1096+
data: repoData,
1097+
context: Context().withEntry(
1098+
HttpLinkResponseContext(
1099+
statusCode: 200,
1100+
headers: {'foo': 'bar'},
1101+
),
1102+
),
1103+
response: {},
1104+
),
1105+
]),
1106+
);
1107+
1108+
client = client.copyWith(link: link);
1109+
final QueryResult r = await client.query(_options);
1110+
1111+
expect(r.exception, isNull);
1112+
expect(r.data, equals(repoData));
1113+
1114+
expect(
1115+
r.context.entry<HttpLinkResponseContext>()!.statusCode,
1116+
equals(200),
1117+
);
1118+
expect(
1119+
r.context.entry<HttpLinkResponseContext>()!.headers['foo'],
1120+
equals('bar'),
1121+
);
1122+
});
1123+
});
10511124
}

packages/graphql_flutter/test/widgets/subscription_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ void main() {
114114
verifyNoMoreInteractions(mockHttpClient);
115115
});
116116
});
117-
}
117+
}

0 commit comments

Comments
 (0)