Milan Surelia
Milan Surelia

Reputation: 1076

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:

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?

Upvotes: 6

Views: 3048

Answers (2)

Sergey  Molchanovsky
Sergey Molchanovsky

Reputation: 643

You should receive a mutation result or nothing (depends on your contract), but you got <!DOCTYPE html> instead. It's definitely wrong, you got an html page redirecting you to login page, instead of a json answer.

Upvotes: 0

Pratik Butani
Pratik Butani

Reputation: 62411

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.

Upvotes: 2

Related Questions