使用 graphql_flutter 包的 mutate 方法时出现此错误。
尝试使用以下版本的qraphql_flutter包:
错误:
I/flutter (13946): //// EXCEPTION: OperationException(linkException: ResponseFormatException(originalException: FormatException: Unexpected character (at character 1)
I/flutter (13946): <!DOCTYPE html>
I/flutter (13946): ^
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='https://xxx.xxx.dev/login'" />
<title>Redirecting to https://xxx.xxx.dev/login</title>
</head>
<body>
Redirecting to <a href="https://xxx.xxx.dev/login">https://xxx.xxx.dev/login</a>.
</body>
</html>
I/flutter (13946): ), graphqlErrors: [])
我尝试使用相同的代码运行查询。此代码与查询完美配合,它仅在使用突变时引发异常。我创建了一个 graphql 帮助器类,它可以帮助使用此帮助器类执行项目中的每个 graphql 操作。
GraphQL 助手类:
import 'package:flutter/foundation.dart';
import 'package:graphql_demo/app_exception.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
class AppGraphQlClient {
late GraphQLClient _client;
AppGraphQlClient(String graphqlUrl) {
final httpLink = HttpLink(graphqlUrl);
_client = GraphQLClient(
link: AuthorizationLink(
}).concat(httpLink),
cache: GraphQLCache());
}
/// Perform mutation by passing query string
Stream<Map<String, dynamic>?> mutateString(String query, {required Map<String, dynamic> variables}) {
if (kDebugMode) {
print("MAP $variables");
}
return _client.mutate(MutationOptions(document: gql(query), variables: variables)).asStream().map((result) {
if (kDebugMode) {
print('//// RESULT: ${result.toString()}');
}
if (result.exception != null) {
if (kDebugMode) {
print('//// EXCEPTION: ${result.exception?.toString()}');
print('//// EXCEPTION: ${result.exception?.graphqlErrors}');
}
throw AppException(message: (result.exception !=null)?result.exception.toString():"Error");
}
return result.data;
});
}
}
class AuthorizationLink extends Link {
@override
Stream<Response> request(Request request, [NextLink? forward]) {
String token =
"authentication token goes here";
final header = Map<String, String>();
header['Authorization'] = '''Bearer $token''';
header['app_version'] = '3.2.3';
header['Accept-Language'] = 'en';
return forward!(request);
}
}
任何人都可以为此提供解决方案吗?