Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter GraphQL - OperationException(linkException: ResponseFormatException(originalException: FormatException: Unexpected character (at character 1)

I am getting this error while using mutate method of graphql_flutter package.

Tried with following versions of qraphql_flutter package:

  • 5.0.1-beta.1
  • 5.0.0
  • 4.0.0-beta.5

Error:

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: [])

I tried running query using the same code.This code works perfectly fine with query, it only throws exception when using mutation.I created a graphql helper class which can help perform every graphql operation in project using this helper class.

GraphQL Helper 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);
  }
}

Can anyone provide a solution for this?

like image 349
Milan Surelia Avatar asked Dec 15 '21 09:12

Milan Surelia


1 Answers

As you said, If you are getting error for mutation only, May be you are passing wrong token or wrong data.

Please check twice your code, must check token is passed whether it is valid or not.

like image 55
Pratik Butani Avatar answered Oct 24 '22 04:10

Pratik Butani