-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquery_manager.dart
206 lines (165 loc) · 5.62 KB
/
query_manager.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
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:flutter_graphql/src/core/query_options.dart';
import 'package:flutter_graphql/src/core/query_result.dart';
import 'package:flutter_graphql/src/core/graphql_error.dart';
import 'package:flutter_graphql/src/core/observable_query.dart';
import 'package:flutter_graphql/src/scheduler/scheduler.dart';
import 'package:flutter_graphql/src/link/link.dart';
import 'package:flutter_graphql/src/link/operation.dart';
import 'package:flutter_graphql/src/link/fetch_result.dart';
import 'package:flutter_graphql/src/cache/cache.dart';
import 'package:flutter_graphql/src/utilities/get_from_ast.dart';
class QueryManager {
QueryManager({
@required this.link,
@required this.cache,
}) {
scheduler = QueryScheduler(
queryManager: this,
);
}
final Link link;
final Cache cache;
QueryScheduler scheduler;
int idCounter = 1;
Map<String, ObservableQuery> queries = <String, ObservableQuery>{};
ObservableQuery watchQuery(WatchQueryOptions options) {
if (options.document == null) {
throw Exception(
'document option is required. You must specify your GraphQL document in the query options.',
);
}
final ObservableQuery observableQuery = ObservableQuery(
queryManager: this,
options: options,
);
setQuery(observableQuery);
return observableQuery;
}
Future<QueryResult> query(QueryOptions options) {
return fetchQuery('0', options);
}
Future<QueryResult> mutate(MutationOptions options) {
return fetchQuery('0', options);
}
Future<QueryResult> fetchQuery(
String queryId,
BaseOptions options,
) async {
final ObservableQuery observableQuery = getQuery(queryId);
// XXX there is a bug in the `graphql_parser` package, where this result might be
// null event though the operation name is present in the document
final String operationName = getOperationName(options.document);
// create a new operation to fetch
final Operation operation = Operation(
document: options.document,
variables: options.variables,
operationName: operationName,
);
FetchResult fetchResult;
QueryResult queryResult;
try {
if (options.context != null) {
operation.setContext(options.context);
}
if (options.fetchPolicy == FetchPolicy.cacheFirst ||
options.fetchPolicy == FetchPolicy.cacheAndNetwork ||
options.fetchPolicy == FetchPolicy.cacheOnly) {
final dynamic cachedData = cache.read(operation.toKey());
if (cachedData != null) {
fetchResult = FetchResult(
data: cachedData,
);
queryResult = _mapFetchResultToQueryResult(fetchResult);
// add the result to an observable query if it exists
if (observableQuery != null) {
observableQuery.controller.add(queryResult);
}
if (options.fetchPolicy == FetchPolicy.cacheFirst ||
options.fetchPolicy == FetchPolicy.cacheOnly) {
return queryResult;
}
}
if (options.fetchPolicy == FetchPolicy.cacheOnly) {
throw Exception(
'Could not find that operation in the cache. (${options.fetchPolicy.toString()})',
);
}
}
// execute the operation through the provided link(s)
fetchResult = await execute(
link: link,
operation: operation,
).first;
// save the data from fetchResult to the cache
if (fetchResult.data != null &&
options.fetchPolicy != FetchPolicy.noCache) {
cache.write(
operation.toKey(),
fetchResult.data,
);
}
if (fetchResult.data == null &&
fetchResult.errors == null &&
(options.fetchPolicy == FetchPolicy.noCache ||
options.fetchPolicy == FetchPolicy.networkOnly)) {
throw Exception(
'Could not resolve that operation on the network. (${options.fetchPolicy.toString()})',
);
}
queryResult = _mapFetchResultToQueryResult(fetchResult);
} catch (error) {
// TODO some dart errors break this
final GraphQLError graphQLError = GraphQLError(
message: error.message,
);
if (queryResult != null) {
queryResult.addError(graphQLError);
} else {
queryResult = QueryResult(
loading: false,
);
queryResult.addError(graphQLError);
}
}
// add the result to an observable query if it exists and not closed
if (observableQuery != null && !observableQuery.controller.isClosed) {
observableQuery.controller.add(queryResult);
}
return queryResult;
}
ObservableQuery getQuery(String queryId) {
if (queries.containsKey(queryId)) {
return queries[queryId];
}
return null;
}
void setQuery(ObservableQuery observableQuery) {
queries[observableQuery.queryId] = observableQuery;
}
void closeQuery(ObservableQuery observableQuery, {bool fromQuery = false}) {
if (!fromQuery) {
observableQuery.close(fromManager: true);
}
queries.remove(observableQuery.queryId);
}
int generateQueryId() {
final int requestId = idCounter;
idCounter++;
return requestId;
}
QueryResult _mapFetchResultToQueryResult(FetchResult fetchResult) {
List<GraphQLError> errors;
if (fetchResult.errors != null) {
errors = List<GraphQLError>.from(fetchResult.errors.map<GraphQLError>(
(dynamic rawError) => GraphQLError.fromJSON(rawError),
));
}
return QueryResult(
data: fetchResult.data,
errors: errors,
loading: false,
);
}
}