/
logging_extension.dart
227 lines (203 loc) · 6.21 KB
/
logging_extension.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
import 'dart:async';
import 'dart:convert' show json;
import 'package:clock/clock.dart';
import 'package:leto/leto.dart';
import 'package:leto_schema/leto_schema.dart';
/// A callback for logging a [GraphQLLog] from a request execution
typedef LogFunction = void Function(GraphQLLog);
/// A log with information about the execution of GraphQL requests
/// or subscription events
class GraphQLLog {
/// The base request context
final RequestCtx baseCtx;
/// The main execution context for the execution, null if there
/// was an error before execution
final ExecutionCtx? ctx;
/// The [GraphQLResult] for the execution, null if there
/// was an error before execution
final GraphQLResult? result;
/// Whether the log is from a subscription event
final bool isSubscriptionEvent;
/// The start time of execution
final DateTime startTime;
/// The duration of execution
final Duration duration;
/// The [GraphQLError]s found during execution, null if there
/// was an error before execution
List<GraphQLError>? get graphQLErrors => result?.errors ?? ctx?.errors;
/// The operation name of the request, null if there
/// was an error before execution
String? get operationName =>
ctx?.operation.name?.value ?? baseCtx.operationName;
/// The source error
///
/// Almost always null. Most of the times, if there were any errors,
/// they will be in [graphQLErrors].
/// More information in [GraphQL.parseAndExecute].
final Object? error;
/// The [StackTrace] for [error]
final StackTrace? stackTrace;
/// A log with information about the execution of GraphQL requests
/// or subscription events
const GraphQLLog({
this.isSubscriptionEvent = false,
required this.startTime,
required this.duration,
required this.result,
required this.ctx,
required this.baseCtx,
this.error,
this.stackTrace,
});
/// Returns the log as a Json Map
///
/// Uses `toJson` for the error or `toString` if not available
/// If [withVariables] is true, it will also return the
/// input variables within the Map
Map<String, Object?> toJson({bool withVariables = false}) {
final type = ctx?.operation.type.toString().split('.').last ?? 'unknown';
return {
'time': startTime.toIso8601String(),
'dur': duration.inMilliseconds,
if (operationName != null) 'operation': operationName,
'type': isSubscriptionEvent ? 'subscription_event' : type,
if (error != null)
'error': () {
try {
// ignore: avoid_dynamic_calls
return (error as dynamic).toJson();
} catch (_) {
return error.toString();
}
}(),
if (stackTrace != null) 'stackTrace': stackTrace.toString(),
'result': () {
if (result == null) return null;
final jsonResult = result!.toJson();
if (jsonResult['data'] is Stream) {
jsonResult['data'] = 'Stream<GraphQLResult>';
}
return jsonResult;
}(),
if (baseCtx.extensions != null) 'extensions': baseCtx.extensions,
if (withVariables) 'variables': baseCtx.rawVariableValues,
'query': baseCtx.query,
};
}
/// A String with the log information separated by [separator]
String separatedLog({
bool withVariables = false,
String separator = '\t',
bool stringifyValues = true,
bool withKeys = true,
}) =>
toJson(withVariables: withVariables).entries.map((e) {
final value = stringifyValues
? e.value is Map
? json.encode(e.value)
: e.value
: e.value;
return withKeys ? '${e.key}=$value' : value;
}).join(separator);
/// A Json String with the log information.
/// More info in [toJson]
String toJsonString({bool withVariables = false}) =>
json.encode(toJson(withVariables: withVariables));
@override
String toString() {
return json.encode(this);
}
}
/// Extension that logs all requests and subscription events
class LoggingExtension extends GraphQLExtension {
/// Extension that logs all requests and subscription events
LoggingExtension(
this.logFunction, {
this.onResolverError,
});
@override
String get mapKey => 'letoLogger';
/// Called for each request or subscription event
final LogFunction logFunction;
/// Optional callback for logging errors thrown in resolvers
final void Function(ThrownError)? onResolverError;
@override
FutureOr<GraphQLResult> executeRequest(
FutureOr<GraphQLResult> Function() next,
RequestCtx ctx,
) async {
final startTime = clock.now();
final watch = clock.stopwatch()..start();
try {
final result = await next();
logFunction(
GraphQLLog(
startTime: startTime,
duration: watch.elapsed,
result: result,
ctx: GraphQL.getResolveCtx(ctx),
baseCtx: ctx,
),
);
return result;
} catch (e, s) {
logFunction(
GraphQLLog(
startTime: startTime,
duration: watch.elapsed,
result: null,
ctx: GraphQL.getResolveCtx(ctx),
baseCtx: ctx,
error: e,
stackTrace: s,
),
);
rethrow;
}
}
@override
FutureOr<GraphQLResult> executeSubscriptionEvent(
FutureOr<GraphQLResult> Function() next,
ExecutionCtx ctx,
ScopedMap parentGlobals,
) async {
final startTime = clock.now();
final watch = clock.stopwatch()..start();
try {
final result = await next();
logFunction(
GraphQLLog(
startTime: startTime,
duration: watch.elapsed,
isSubscriptionEvent: true,
result: result,
ctx: ctx,
baseCtx: ctx.requestCtx,
),
);
return result;
} catch (e, s) {
logFunction(
GraphQLLog(
startTime: startTime,
duration: watch.elapsed,
isSubscriptionEvent: true,
result: null,
ctx: ctx,
baseCtx: ctx.requestCtx,
error: e,
stackTrace: s,
),
);
rethrow;
}
}
@override
GraphQLException mapException(
GraphQLException Function() next,
ThrownError error,
) {
onResolverError?.call(error);
return next();
}
}