Flutter GraphQL - OperationException(linkException:ResponseFormatException(originalException:FormatException:意外字符(在字符1处)
Flutter GraphQL - OperationException(linkException: ResponseFormatException(originalException: FormatException: Unexpected character (at character 1)
我在使用 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: [])
我尝试使用相同的 code.This 代码进行 运行 查询,查询效果非常好,它只会在使用 mutation.I 创建一个 graphql 助手 class 时抛出异常,它可以使用此助手帮助执行项目中的每个 graphql 操作 class.
GraphQL 助手 Class:
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);
}
}
任何人都可以为此提供解决方案吗?
如您所说,如果您仅收到突变错误,可能是您传递了错误的令牌或数据。
请检查您的代码两次,必须检查令牌是否有效。
我在使用 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: [])
我尝试使用相同的 code.This 代码进行 运行 查询,查询效果非常好,它只会在使用 mutation.I 创建一个 graphql 助手 class 时抛出异常,它可以使用此助手帮助执行项目中的每个 graphql 操作 class.
GraphQL 助手 Class:
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);
}
}
任何人都可以为此提供解决方案吗?
如您所说,如果您仅收到突变错误,可能是您传递了错误的令牌或数据。
请检查您的代码两次,必须检查令牌是否有效。