mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-08 17:11:14 +00:00
Merge pull request 'feat: Implement logging for GraphQL API map' (#153) from graphql-logging into master
Reviewed-on: https://git.selfprivacy.org/kherel/selfprivacy.org.app/pulls/153 Reviewed-by: Inex Code <inex.code@selfprivacy.org>
This commit is contained in:
commit
a0e531f18c
|
@ -4,6 +4,42 @@ import 'package:graphql_flutter/graphql_flutter.dart';
|
|||
import 'package:http/io_client.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.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 RequestLoggingLink extends Link {
|
||||
@override
|
||||
Stream<Response> request(
|
||||
final Request request, [
|
||||
final NextLink? forward,
|
||||
]) async* {
|
||||
_logToAppConsole(request);
|
||||
yield* forward!(request);
|
||||
}
|
||||
}
|
||||
|
||||
class ResponseLoggingParser 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 {
|
||||
Future<GraphQLClient> getClient() async {
|
||||
|
@ -22,16 +58,23 @@ abstract class ApiMap {
|
|||
final httpLink = HttpLink(
|
||||
'https://api.$rootAddress/graphql',
|
||||
httpClient: ioClient,
|
||||
parser: ResponseLoggingParser(),
|
||||
);
|
||||
|
||||
final String token = _getApiToken();
|
||||
|
||||
final Link graphQLLink = isWithToken
|
||||
? AuthLink(
|
||||
getToken: () async =>
|
||||
customToken == '' ? 'Bearer $token' : customToken,
|
||||
).concat(httpLink)
|
||||
: httpLink;
|
||||
final Link graphQLLink = RequestLoggingLink().concat(
|
||||
isWithToken
|
||||
? AuthLink(
|
||||
getToken: () async =>
|
||||
customToken == '' ? 'Bearer $token' : customToken,
|
||||
).concat(httpLink)
|
||||
: httpLink,
|
||||
);
|
||||
|
||||
// Every request goes through either chain:
|
||||
// 1. RequestLoggingLink -> AuthLink -> HttpLink
|
||||
// 2. RequestLoggingLink -> HttpLink
|
||||
|
||||
return GraphQLClient(
|
||||
cache: GraphQLCache(),
|
||||
|
|
Loading…
Reference in a new issue