-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
Copy pathgraphql_client.dart
201 lines (188 loc) · 5.81 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
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.
///
///
/// [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 {
/// Constructs a [GraphQLClient] given a [Link] and a [Cache].
GraphQLClient({
@required this.link,
@required this.cache,
this.defaultPolicies,
}) {
defaultPolicies ??= DefaultPolicies();
queryManager = QueryManager(
link: link,
cache: cache,
);
}
/// The default [Policies] to set for each client action
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;
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);
}
/// 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('Rew 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].
@experimental
Future<QueryResult> fetchMore(
FetchMoreOptions fetchMoreOptions, {
@required QueryOptions originalOptions,
@required QueryResult previousResult,
}) =>
fetchMoreImplementation(
fetchMoreOptions,
originalOptions: originalOptions,
previousResult: previousResult,
queryManager: queryManager,
);
}