我想从 data.errors 中获取我发送的错误。我使用“https://github.com/zino-app/graphql-flutter/tree/master/packages/graphql”来获得我的回复。我的回复如下所示:
{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
我获取数据的代码如下所示:
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;
}
}
}
有人可以在这里帮助我吗?result.exception 不会向我显示想要的错误。