【问题标题】:Flutter GraphQL - OperationException(linkException: ResponseFormatException(originalException: FormatException: Unexpected character (at character 1)Flutter GraphQL - OperationException(linkException:ResponseFormatException(originalException:FormatException:意外字符(在字符1)
【发布时间】:2022-01-18 11:33:09
【问题描述】:

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

谁能为此提供解决方案?

【问题讨论】:

    标签: flutter graphql flutter-graphql graphql-flutter


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 2019-09-04
      • 1970-01-01
      • 2021-08-29
      • 2021-03-30
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多