简体   繁体   中英

Flutter OperationException(linkException: ResponseFormatException(originalException: FormatException: Unexpected end of input (at character 1)

Previously I was using graphql version 3.1.0 and it was working fine, able to receive request as expected, but recently I had to update my flutter version to 2.10.3 and all other packages, including graphql.

Now I am using v.5.1.0 and I suddenly get this error.

Only changed the documentNode to document in Query. I built the request as per the documentation.

main.dart

 @override
  void initState() {
    _appSettingsManager.loadSettings();
    GraphQLRepository.instance.initializeClient();
    super.initState();
  }

GraphQLProvider(client: GraphQLRepository.instance.graphQLClientValueNotifier,child: MaterialApp....

Graphql Repository

class GraphQLRepository {
  factory GraphQLRepository() {
    return instance;
  }

  GraphQLRepository._();

  static final GraphQLRepository instance = GraphQLRepository._();
  GraphQLClient _graphQLClient;
  ValueNotifier<GraphQLClient> _graphQLClientValueNotifier;

  GraphQLClient get graphQlClient => _graphQLClient;
  ValueNotifier<GraphQLClient> get graphQLClientValueNotifier =>
      _graphQLClientValueNotifier;

  void initializeClient() {
    _graphQLClientValueNotifier = GraphqlClient.initializeClient();
    _graphQLClient = _graphQLClientValueNotifier.value;
    return;
  }
}

Client

class GraphqlClient {
  static String _token;


  static final HttpLink httpLink = HttpLink(
     serverUrl,
  );

  static final AuthLink authLink = AuthLink(getToken: () async {
    _token = 'Bearer $accountKey, Bearer $sessionKey';
    return _token ?? '';
  });

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

  static ValueNotifier<GraphQLClient> initializeClient() {
    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;
  }
}

Response

{
  "data": {
    "balanceList": {
      "data": [
        {
          "currency": "GBP",
          "amount": 1000,
          "pendingAmount": 199,
          "holdAmount": 0,
          "availableBalance": 801
        },
        {
          "currency": "EUR",
          "amount": 1000000,
          "pendingAmount": 647,
          "holdAmount": 0,
          "availableBalance": 999353
        }
      ]
    }
  }
}

Previously I was using graphql version 3.1.0 and it was working fine, able to receive request as expected, but recently I had to update my flutter version to 2.10.3 and all other packages, including graphql.

Welcome to the present.

Try to look in the migrate guide https://github.com/zino-hofmann/graphql-flutter/blob/main/changelog-v3-v4.md this will solve your problem!

In addition, your query needs to contain __typename as described in the example https://github.com/zino-hofmann/graphql-flutter/blob/main/packages/graphql/README.md#query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM