fluttergraphqlflutter-graphqlgraphql-flutter

Flutter graphql show actual request


I'm using flutter_graphql and keep getting exception OperationException(linkException: ResponseFormatException(originalException: FormatException: Unexpected end of input (at character 1), )^ graphqlErrors: []),

Is there a way to show the actual request sent? Want to see in console the actual request passed

Client

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;
  }
}

Request

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

Query

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
          }
        }
      }
    }
  }
      """

Solution

  • you can create a Link that logs every request you sent and then concat it with your Link

    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();
    } 
    

    you can create instance of this link

    final _loggerLink = LoggerLink() ;
    

    then you will concat it in your client

    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),
          ),
        ));
    

    note that

    httpLink
    

    is your actual link