-
-
Notifications
You must be signed in to change notification settings - Fork 635
/
Copy pathquery.dart
165 lines (134 loc) · 3.18 KB
/
query.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
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:graphql_flutter/src/client.dart';
import 'package:graphql_flutter/src/widgets/graphql_provider.dart';
typedef Widget QueryBuilder({
@required bool loading,
Map<String, dynamic> data,
Exception error,
});
class Query extends StatefulWidget {
Query(
this.query, {
final Key key,
this.variables = const {},
@required this.builder,
this.pollInterval,
}) : super(key: key);
final String query;
final Map<String, dynamic> variables;
final QueryBuilder builder;
final int pollInterval;
@override
QueryState createState() => QueryState();
}
class QueryState extends State<Query> {
Client client;
bool loading = true;
Map<String, dynamic> data = {};
Exception error;
bool initialFetch = true;
Duration pollInterval;
Timer pollTimer;
Map currentVariables = Map();
@override
void initState() {
super.initState();
if (widget.pollInterval is int) {
pollInterval = Duration(seconds: widget.pollInterval);
}
}
@override
void didChangeDependencies() {
/// Gets the client from the closest wrapping [GraphqlProvider].
client = GraphqlProvider.of(context).value;
super.didChangeDependencies();
}
@override
void dispose() {
_deleteTimer();
super.dispose();
}
void _deleteTimer() {
if (pollTimer is Timer) {
pollTimer.cancel();
pollTimer = null;
}
}
void getQueryResult() async {
assert(client != null);
try {
final Map<String, dynamic> result = client.readQuery(
query: widget.query,
variables: widget.variables,
);
if (this.mounted) {
setState(() {
data = result;
error = null;
loading = false;
});
}
} catch (e) {
// Ignore, right?
}
try {
final Map<String, dynamic> result = await client.query(
query: widget.query,
variables: widget.variables,
);
if (this.mounted) {
setState(() {
data = result;
error = null;
loading = false;
});
}
} catch (e) {
if (data.isEmpty) {
if (this.mounted) {
setState(() {
error = e;
loading = false;
});
}
}
}
if (pollInterval is Duration && !(pollTimer is Timer)) {
pollTimer = Timer.periodic(
pollInterval,
(Timer t) => getQueryResult(),
);
}
}
bool _areDifferentMaps(Map a, Map b) {
if (a.length != b.length) {
return true;
}
bool areDifferent = false;
a.forEach((key, value) {
if (b[key] != a[key] || (!b.containsKey(key))) {
areDifferent = true;
}
});
return areDifferent;
}
Widget build(BuildContext context) {
if (initialFetch) {
initialFetch = false;
currentVariables = widget.variables;
getQueryResult();
}
if (_areDifferentMaps(currentVariables, widget.variables)) {
currentVariables = widget.variables;
loading = true;
_deleteTimer();
getQueryResult();
}
return widget.builder(
loading: loading,
error: error,
data: data,
);
}
}