7

使用 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);
  }
}

任何人都可以为此提供解决方案吗?

4

1 回答 1

0

正如您所说,如果您仅收到突变错误,则可能是您传递了错误的令牌或错误的数据。

请检查两次您的代码,必须检查令牌是否有效。

于 2021-12-16T11:10:21.997 回答