-
-
Notifications
You must be signed in to change notification settings - Fork 623
/
main.dart
286 lines (260 loc) · 9.22 KB
/
main.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import '../graphql_operation/mutations/mutations.dart' as mutations;
import '../graphql_operation/queries/readRepositories.dart' as queries;
import '../helpers.dart' show withGenericHandling;
// to run the example, replace <YOUR_PERSONAL_ACCESS_TOKEN> with your GitHub token in ../local.dart
import '../local.dart';
class GraphQLWidgetScreen extends StatelessWidget {
const GraphQLWidgetScreen() : super();
@override
Widget build(BuildContext context) {
var httpLink = HttpLink('https://api.github.com/graphql', defaultHeaders: {
'Authorization': 'Bearer $YOUR_PERSONAL_ACCESS_TOKEN',
});
final client = ValueNotifier<GraphQLClient>(
GraphQLClient(
cache: GraphQLCache(),
link: httpLink,
),
);
return GraphQLProvider(
client: client,
child: const CacheProvider(
child: MyHomePage(title: 'GraphQL Widget'),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
this.title,
}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int nRepositories = 50;
void changeQuery(String number) {
setState(() {
nRepositories = int.parse(number);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title!),
),
body: Container(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
TextField(
decoration: const InputDecoration(
labelText: 'Number of repositories (default 50)',
),
keyboardType: TextInputType.number,
onSubmitted: changeQuery,
),
Query(
options: QueryOptions(
document: gql(queries.readRepositories),
variables: {
'nRepositories': nRepositories,
},
//pollInterval: 10,
),
builder: withGenericHandling(
(QueryResult result, {refetch, fetchMore}) {
if (result.data == null && !result.hasException) {
return const Text(
'Loading has completed, but both data and errors are null. '
'This should never be the case – please open an issue',
);
}
// result.data can be either a [List<dynamic>] or a [Map<String, dynamic>]
final repositories = (result.data!['viewer']['repositories']
['nodes'] as List<dynamic>);
return Expanded(
child: ListView.builder(
itemCount: repositories.length,
itemBuilder: (BuildContext context, int index) {
return StarrableRepository(
repository: repositories[index],
optimistic: result.source ==
QueryResultSource.optimisticResult,
);
},
),
);
},
),
),
],
),
),
);
}
}
class StarrableRepository extends StatelessWidget {
const StarrableRepository({
Key? key,
required this.repository,
required this.optimistic,
}) : super(key: key);
final Map<String, dynamic> repository;
final bool optimistic;
/// Extract the repository data for updating the fragment
Map<String, dynamic>? extractRepositoryData(Map<String, dynamic> data) {
final action = data['action'] as Map<String, dynamic>?;
if (action == null) {
return null;
}
return action['starrable'] as Map<String, dynamic>?;
}
/// Get whether the repository is currently starred, according to the current Query
bool? get starred => repository['viewerHasStarred'] as bool?;
/// Build an optimisticResult based on whether [viewerIsStarrring]
Map<String, dynamic> expectedResult(bool viewerIsStarrring) =>
<String, dynamic>{
'action': {
'starrable': {
'__typename': 'Repository',
'id': repository['id'],
'viewerHasStarred': viewerIsStarrring,
}
}
};
OnMutationUpdate get update => (cache, result) {
if (result!.hasException) {
print(result.exception);
} else {
final updated = {
...repository,
...extractRepositoryData(result.data!)!,
};
cache.writeFragment(
Fragment(
document: gql(
'''
fragment fields on Repository {
id
name
viewerHasStarred
}
''',
),
).asRequest(idFields: {
'__typename': updated['__typename'],
'id': updated['id'],
}),
data: updated,
);
}
};
@override
Widget build(BuildContext context) {
/// While we could toggle between the addStar and removeStar mutations conditionally,
/// this would discard and rebuild each associated [ObservableQuery]. The side effects would still execute,
/// but we would not have a way to inspect the mutation results, such as with [_debugLatestResults].
return Mutation(
options: MutationOptions(
document: gql(mutations.addStar),
update: update,
onError: (OperationException? error) =>
_simpleAlert(context, error.toString()),
onCompleted: (dynamic resultData) =>
_simpleAlert(context, 'Thanks for your star!'),
// 'Sorry you changed your mind!',
),
builder: (RunMutation _addStar, QueryResult? addResult) {
final addStar = () => _addStar({'starrableId': repository['id']},
optimisticResult: expectedResult(true));
return Mutation(
options: MutationOptions(
document: gql(mutations.removeStar),
update: update,
onError: (OperationException? error) =>
_simpleAlert(context, error.toString()),
onCompleted: (dynamic resultData) =>
_simpleAlert(context, 'Sorry you changed your mind!'),
),
builder: (RunMutation _removeStar, QueryResult? removeResult) {
final removeStar = () => _removeStar(
{'starrableId': repository['id']},
optimisticResult: expectedResult(false));
final anyLoading =
addResult!.isLoading || removeResult!.isLoading || optimistic;
return ListTile(
leading: starred!
? Icon(
Icons.star,
color: Colors.amber,
)
: Icon(Icons.star_border),
trailing: anyLoading ? CircularProgressIndicator() : null,
title: Text(repository['name'] as String),
/// uncomment this line to see the actual mutation results
subtitle: _debugLatestResults(addResult, removeResult!),
onTap: anyLoading
? null
: starred!
? removeStar
: addStar,
);
},
);
},
);
}
// TODO extract these details into better docs on [Policies]
/// Used for inspecting the mutation results.
///
/// Can be used to observe the behavior in https://github.com/zino-app/graphql-flutter/issues/774,
/// patched in https://github.com/zino-app/graphql-flutter/pull/795 with the addition of [CacheRereadPolicy].
///
/// To behavior, add the following to the `Mutations` above:
/// ```dart
/// fetchPolicy: FetchPolicy.networkOnly,
/// cacheRereadPolicy: CacheRereadPolicy.mergeOptimistic,
/// ```
/// This will cause the mutation results to be rebroadcast from the cache,
/// merging in the new `Repository.viewerHasStarred` state.
/// This can be desirable when a mutation result is used merely as a follow-up query.
Widget? _debugLatestResults(QueryResult add, QueryResult remove) {
//return null;
var latestResults = '';
if (add.data != null) {
latestResults += 'addResultRepo: ${extractRepositoryData(add.data!)}; ';
}
if (remove.data != null) {
latestResults +=
'removeResultRepo: ${extractRepositoryData(remove.data!)}; ';
}
if (latestResults.isEmpty) {
return null;
}
return Text(latestResults);
}
}
void _simpleAlert(BuildContext context, String text) => showDialog<AlertDialog>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(text),
actions: <Widget>[
SimpleDialogOption(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('DISMISS'),
)
],
);
},
);