-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
Copy pathgraphql_client.dart
266 lines (247 loc) · 8.31 KB
/
graphql_client.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import 'package:meta/meta.dart';
import 'dart:async';
import 'package:graphql/src/core/core.dart';
import 'package:graphql/src/cache/cache.dart';
import 'package:graphql/src/core/fetch_more.dart';
/// Universal GraphQL Client with configurable caching and [link][] system.
/// modelled after the [`apollo-client`][ac].
///
/// The link is a [Link] over which GraphQL documents will be resolved into a [Response].
/// The cache is the [GraphQLCache] to use for caching results and optimistic updates.
///
/// The client automatically rebroadcasts watched queries when their underlying data
/// changes in the cache. To skip the data comparison check, `alwaysRebroadcast: true` can be passed.
/// **NOTE**: This flag was added ot accomodate the old default behavior.
/// It is marked `@experimental` because it may be deprecated in the future.
///
/// [ac]: https://www.apollographql.com/docs/react/v3.0-beta/api/core/ApolloClient/
/// [link]: https://github.com/gql-dart/gql/tree/master/links/gql_link
class GraphQLClient implements GraphQLDataProxy {
/// Constructs a [GraphQLClient] given a [Link] and a [Cache].
GraphQLClient({
required this.link,
required this.cache,
DefaultPolicies? defaultPolicies,
bool alwaysRebroadcast = false,
}) : defaultPolicies = defaultPolicies ?? DefaultPolicies(),
queryManager = QueryManager(
link: link,
cache: cache,
alwaysRebroadcast: alwaysRebroadcast,
);
/// The default [Policies] to set for each client action
late final DefaultPolicies defaultPolicies;
/// The [Link] over which GraphQL documents will be resolved into a [Response].
final Link link;
/// The initial [Cache] to use in the data store.
final GraphQLCache cache;
late final QueryManager queryManager;
/// This registers a query in the [QueryManager] and returns an [ObservableQuery]
/// based on the provided [WatchQueryOptions].
///
/// {@tool snippet}
/// Basic usage
///
/// ```dart
/// final observableQuery = client.watchQuery(
/// WatchQueryOptions(
/// document: gql(
/// r'''
/// query HeroForEpisode($ep: Episode!) {
/// hero(episode: $ep) {
/// name
/// }
/// }
/// ''',
/// ),
/// variables: {'ep': 'NEWHOPE'},
/// ),
/// );
///
/// /// Listen to the stream of results. This will include:
/// /// * `options.optimisitcResult` if passed
/// /// * The result from the server (if `options.fetchPolicy` includes networking)
/// /// * rebroadcast results from edits to the cache
/// observableQuery.stream.listen((QueryResult result) {
/// if (!result.isLoading && result.data != null) {
/// if (result.hasException) {
/// print(result.exception);
/// return;
/// }
/// if (result.isLoading) {
/// print('loading');
/// return;
/// }
/// doSomethingWithMyQueryResult(myCustomParser(result.data));
/// }
/// });
/// // ... cleanup:
/// observableQuery.close();
/// ```
/// {@end-tool}
ObservableQuery watchQuery(WatchQueryOptions options) {
options.policies =
defaultPolicies.watchQuery.withOverrides(options.policies);
return queryManager.watchQuery(options);
}
/// [watchMutation] is the same as [watchQuery], but with a different [defaultPolicies] that are more appropriate for mutations.
///
/// This is a stop-gap solution to the problems created by the reliance of `graphql_flutter` on [ObservableQuery] for mutations.
///
/// For more details, see https://github.com/zino-app/graphql-flutter/issues/774
ObservableQuery watchMutation(WatchQueryOptions options) {
options.policies =
defaultPolicies.watchMutation.withOverrides(options.policies);
return queryManager.watchQuery(options);
}
/// This resolves a single query according to the [QueryOptions] specified and
/// returns a [Future] which resolves with the [QueryResult] or throws an [Exception].
///
/// {@tool snippet}
/// Basic usage
///
/// ```dart
/// final QueryResult result = await client.query(
/// QueryOptions(
/// document: gql(
/// r'''
/// query ReadRepositories($nRepositories: Int!) {
/// viewer {
/// repositories(last: $nRepositories) {
/// nodes {
/// __typename
/// id
/// name
/// viewerHasStarred
/// }
/// }
/// }
/// }
/// ''',
/// ),
/// variables: {
/// 'nRepositories': 50,
/// },
/// ),
/// );
///
/// if (result.hasException) {
/// print(result.exception.toString());
/// }
///
/// final List<dynamic> repositories =
/// result.data['viewer']['repositories']['nodes'] as List<dynamic>;
/// ```
/// {@end-tool}
Future<QueryResult> query(QueryOptions options) {
options.policies = defaultPolicies.query.withOverrides(options.policies);
return queryManager.query(options);
}
/// This resolves a single mutation according to the [MutationOptions] specified and
/// returns a [Future] which resolves with the [QueryResult] or throws an [Exception].
Future<QueryResult> mutate(MutationOptions options) {
options.policies = defaultPolicies.mutate.withOverrides(options.policies);
return queryManager.mutate(options);
}
/// This subscribes to a GraphQL subscription according to the options specified and returns a
/// [Stream] which either emits received data or an error.
///
/// {@tool snippet}
/// Basic usage
///
/// ```dart
/// subscription = client.subscribe(
/// SubscriptionOptions(
/// document: gql(
/// r'''
/// subscription reviewAdded {
/// reviewAdded {
/// stars, commentary, episode
/// }
/// }
/// ''',
/// ),
/// ),
/// );
///
/// subscription.listen((result) {
/// if (result.hasException) {
/// print(result.exception.toString());
/// return;
/// }
///
/// if (result.isLoading) {
/// print('awaiting results');
/// return;
/// }
///
/// print('New Review: ${result.data}');
/// });
/// ```
/// {@end-tool}
Stream<QueryResult> subscribe(SubscriptionOptions options) {
options.policies = defaultPolicies.subscribe.withOverrides(
options.policies,
);
return queryManager.subscribe(options);
}
/// Fetch more results and then merge them with the given [previousResult]
/// according to [FetchMoreOptions.updateQuery].
///
/// **NOTE**: with the addition of strict data structure checking in v4,
/// it is easy to make mistakes in writing [updateQuery].
///
/// To mitigate this, [FetchMoreOptions.partial] has been provided.
@experimental
Future<QueryResult> fetchMore(
FetchMoreOptions fetchMoreOptions, {
required QueryOptions originalOptions,
required QueryResult previousResult,
}) =>
fetchMoreImplementation(
fetchMoreOptions,
originalOptions: originalOptions,
previousResult: previousResult,
queryManager: queryManager,
);
/// pass through to [cache.readQuery]
readQuery(request, {optimistic = true}) =>
cache.readQuery(request, optimistic: optimistic);
/// pass through to [cache.readFragment]
readFragment(
fragmentRequest, {
optimistic = true,
}) =>
cache.readFragment(
fragmentRequest,
optimistic: optimistic,
);
/// pass through to [cache.writeQuery] and then rebroadcast any changes.
void writeQuery(request, {required data, broadcast = true}) {
cache.writeQuery(request, data: data, broadcast: broadcast);
queryManager.maybeRebroadcastQueries();
}
/// pass through to [cache.writeFragment] and then rebroadcast any changes.
void writeFragment(
fragmentRequest, {
broadcast = true,
required data,
}) {
cache.writeFragment(
fragmentRequest,
broadcast: broadcast,
data: data,
);
queryManager.maybeRebroadcastQueries();
}
/// Resets the contents of the store with [cache.store.reset()]
/// and then refetches of all queries unless [refetchQueries] is disabled
@experimental
Future<List<QueryResult?>>? resetStore({bool refetchQueries = true}) {
cache.store.reset();
if (refetchQueries) {
return queryManager.refetchSafeQueries();
}
return null;
}
}