How can I get "errors" from graphql-flutter

2.2k views Asked by At

I want to get my sended errors from data.errors. I use "https://github.com/zino-app/graphql-flutter/tree/master/packages/graphql" to get my response. My response look like this:

{data: {…}, status: 401, statusText: "OK", headers: {…}, config: {…}, …}
config: {url: "http://localhost:3000/graphql", method: "post", data: "{"query":"\n        query getSomeData($date: Str…variables":{"date":"2020-09-18","city":"London"}}", headers: {…}, transformRequest: Array(1), …}
data:
data: {getSomeData: null}
errors: [{…}]
__proto__: Object
headers: {content-length: "88", content-type: "application/json; charset=utf-8"}
original_status: 200
request: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …}
status: 401
statusText: "OK"
__proto__: Object

My code to get my data looks like:

import 'dart:async';

import 'package:graphql/client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';

import '../shared/secure_storage.dart' as storage;

final HttpLink _httpLink = HttpLink(
  uri: DotEnv().env['BACKEND_CONNECTION'],
);

final AuthLink _authLink = AuthLink(
  getToken: () async => 'Bearer ' + storage.getToken(),
);

final Link _link = _authLink.concat(_httpLink);

final GraphQLClient _client = GraphQLClient(
  cache: InMemoryCache(),
  link: _link,
);

final String query = ''' typical query''';

class PublicConcertsBloc extends ChangeNotifier {
  //Data
  List _result;
  List get result => _result;

  //Getters

  QueryOptions get options => QueryOptions(
      documentNode: gql(query), variables: {'var1': var, 'var2': 'var2'});

  set result(List val) {
    _result = val;
    notifyListeners();
  }

  Future loadConcerts() async {
    final QueryResult result = await _client.query(options);

    if (result.data == null) {
      print("data == null");
    } else if (result.data["getSomeData"] == null) {
      print("getSomeData == getSomeData");
    } else if (result.loading) {
      print(result.loading.toString());
    } else if (result.hasException) {
      print(result.exception.toString());
    } else {
      print(result.data['getSomeData']);
      _result = result.data['getSomeData'] as List;
    }
  }
}

Can someone help me here? result.exception dont show me the wanted erros.

1

There are 1 answers

0
Omatt On

The error is provided on the HTTP Response - which is 401. This status code is thrown due to an unauthenticated HTTP Request. The reason why QueryResult.exception is empty was because the HTTP Request was successfully executed.