【问题标题】:Flutter graphql show actual requestFlutter graphql 显示实际请求
【发布时间】:2022-10-22 19:17:42
【问题描述】:

我正在使用flutter_graphql 并不断收到异常OperationException(linkException:ResponseFormatException(originalException:FormatException:输入意外结束(在字符1处),)^ graphqlErrors:[]),

有没有办法显示发送的实际请求? 想在控制台中查看通过的实际请求

客户

class GraphqlClient {
  static String _token;
  static final String serverUrl =
      GlobalConfiguration().getString(Config.GRAPHQL_URL);

  static final HttpLink httpLink = HttpLink(
     serverUrl,
  );

  static final AuthLink authLink = AuthLink(getToken: () async {
    final SharedPrefsRepository _sharedPrefsRepository =
        SharedPrefsRepository();
    String accountKey = await _sharedPrefsRepository.getAccountKey();
    String sessionKey= await _sharedPrefsRepository.getSessionKey();
    _token = 'Bearer $accountKey, Bearer $sessionKey';
    debugPrint('token '+_token);
    return _token ?? '';
  });

  static final Link link = authLink.concat(httpLink);

  static ValueNotifier<GraphQLClient> initializeClient() {
    debugPrint('link '+link.toString());
    final policies = Policies(
      fetch: FetchPolicy.networkOnly,
    );
    
    final ValueNotifier<GraphQLClient> client = ValueNotifier<GraphQLClient>(
      GraphQLClient(
        cache: GraphQLCache(store: HiveStore()),
        link: link,
        defaultPolicies: DefaultPolicies(
          watchQuery: policies,
          query: policies,
          mutate: policies,
        ),
      ),
    );
    return client;
  }
}

要求

Query(
            options: QueryOptions(
              document: gql(DashboardGraphQL.accountDetailsQuery),
              operationName: 'AccountDetails',
            ),

询问

static const String accountDetailsQuery = """
      query AccountDetails {
    accountDetails {
      ... on AccountDetails {
        ibanList {
          accountId
          bicCode
          iban
        }
        accountType
        accountNumber
        accountNumberShort
        accountId
        ruid
        companyId
        accountDataOpened
        email
        mobile
        baseCurrency
        balanceInBaseCurrency
        lastTransactionDate
      }

      ... on ResponseErrors {
        errors {
          message
          code
          displayMessage

          ... on InternalError {
            message
            code
            displayMessage
            context
          }
        }
      }
    }
  }
      """

【问题讨论】:

  • 我想你需要

标签: flutter graphql flutter-graphql graphql-flutter


【解决方案1】:

您可以创建一个链接来记录您发送的每个请求,然后将其与您的链接连接

class LoggerLink extends Link {


@override
  Stream<Response> request(
    Request request, [
    NextLink? forward,
  ]) {
    Stream<Response> response = forward!(request).map((Response fetchResult) {
      final ioStreamedResponse =
          fetchResult.context.entry<HttpLinkResponseContext>();
      if (kDebugMode) {
        print("Request: " + request.toString());
        print("Response:" + (ioStreamedResponse?.toString() ?? "null"));
      }
      return fetchResult;
    }).handleError((error) {
      // throw error;
    });

    return response;
  }

  LoggerLink();
} 

您可以创建此链接的​​实例

final _loggerLink = LoggerLink() ;

然后你将在你的客户端中连接它

client = ValueNotifier<GraphQLClient>(GraphQLClient(
      link: _loggerLink.concat(httpLink),
      cache: GraphQLCache(),
      defaultPolicies: DefaultPolicies(
        watchQuery: Policies(fetch: FetchPolicy.networkOnly),
        query: Policies(fetch: FetchPolicy.networkOnly),
        mutate: Policies(fetch: FetchPolicy.networkOnly),
      ),
    ));

注意

httpLink

是你的实际链接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-21
    • 2011-10-15
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多