mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-23 17:26:35 +00:00
feat: Implement logging for GraphQL API map
Log to application console all requests and all responses
This commit is contained in:
parent
1e8f17f16e
commit
813d275d12
|
@ -1,9 +1,48 @@
|
||||||
|
// ignore_for_file: prefer_foreach
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:graphql_flutter/graphql_flutter.dart';
|
import 'package:graphql_flutter/graphql_flutter.dart';
|
||||||
import 'package:http/io_client.dart';
|
import 'package:http/io_client.dart';
|
||||||
import 'package:selfprivacy/config/get_it_config.dart';
|
import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/staging_options.dart';
|
import 'package:selfprivacy/logic/api_maps/staging_options.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/message.dart';
|
||||||
|
|
||||||
|
void _logToAppConsole<T>(final T objectToLog) {
|
||||||
|
getIt.get<ConsoleModel>().addMessage(
|
||||||
|
Message(
|
||||||
|
text: objectToLog.toString(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoggingLink extends Link {
|
||||||
|
@override
|
||||||
|
Stream<Response> request(
|
||||||
|
final Request request, [
|
||||||
|
final NextLink? forward,
|
||||||
|
]) async* {
|
||||||
|
_logToAppConsole(request);
|
||||||
|
yield* forward!(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoggingResponseParser extends ResponseParser {
|
||||||
|
@override
|
||||||
|
Response parseResponse(final Map<String, dynamic> body) {
|
||||||
|
final response = super.parseResponse(body);
|
||||||
|
_logToAppConsole(response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
GraphQLError parseError(final Map<String, dynamic> error) {
|
||||||
|
final graphQlError = super.parseError(error);
|
||||||
|
_logToAppConsole(graphQlError);
|
||||||
|
return graphQlError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
abstract class ApiMap {
|
abstract class ApiMap {
|
||||||
Future<GraphQLClient> getClient() async {
|
Future<GraphQLClient> getClient() async {
|
||||||
|
@ -22,16 +61,23 @@ abstract class ApiMap {
|
||||||
final httpLink = HttpLink(
|
final httpLink = HttpLink(
|
||||||
'https://api.$rootAddress/graphql',
|
'https://api.$rootAddress/graphql',
|
||||||
httpClient: ioClient,
|
httpClient: ioClient,
|
||||||
|
parser: LoggingResponseParser(),
|
||||||
);
|
);
|
||||||
|
|
||||||
final String token = _getApiToken();
|
final String token = _getApiToken();
|
||||||
|
|
||||||
final Link graphQLLink = isWithToken
|
final Link graphQLLink = LoggingLink().concat(
|
||||||
? AuthLink(
|
isWithToken
|
||||||
getToken: () async =>
|
? AuthLink(
|
||||||
customToken == '' ? 'Bearer $token' : customToken,
|
getToken: () async =>
|
||||||
).concat(httpLink)
|
customToken == '' ? 'Bearer $token' : customToken,
|
||||||
: httpLink;
|
).concat(httpLink)
|
||||||
|
: httpLink,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Every request goes through either chain:
|
||||||
|
// 1. AuthLink -> HttpLink -> LoggingLink
|
||||||
|
// 2. HttpLink -> LoggingLink
|
||||||
|
|
||||||
return GraphQLClient(
|
return GraphQLClient(
|
||||||
cache: GraphQLCache(),
|
cache: GraphQLCache(),
|
||||||
|
|
Loading…
Reference in a new issue