[英]Unable to retrieve errors occured in Graphql mutation in flutter project
I am using the package graphql_flutter for GraphQL operations in my flutter app.我在我的 flutter 应用程序中使用 package graphql_flutter进行 GraphQL 操作。 The queries and mutations are going well but I cannot retrieve the errors by following the ways mentioned in their doc.
查询和突变进展顺利,但我无法按照他们文档中提到的方式检索错误。 Every time I receive a generic error message which is,
每次我收到一般错误消息时,
ClientException: Failed to connect to http://127.0.0.1:3006/graphql:
That I get by doing,我做得到,
print(result.exception.toString());
My mutation looks like,我的突变看起来像,
final MutationOptions mutationOptions = MutationOptions(
documentNode: gql(mutationString),
variables: vars
);
final QueryResult result = await _instance._client.mutate(mutationOptions);
if (result.hasException) {
// none of the following prints the expected error.
print(result.exception.clientException.message);
print(result.exception.graphqlErrors);
print(result.exception.toString());
}
print(result.data);
return result.data;
Whereas in the apollo client, My error is:而在阿波罗客户端,我的错误是:
{
"errors": [
{
"message": "Invalid Phone number provided",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"otp"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
....
But I get none of that.但我什么都没有。
Note: The success response is coming as expected.注意:成功响应如期而至。 I would like to know how can I get the graphql errors.
我想知道如何获得 graphql 错误。
I found the problem.我发现了问题。 It is because android cannot connect to
127.0.0.1
or localhost
from the emulator.这是因为 android 无法从模拟器连接到
127.0.0.1
或localhost
。 I replaced the host with my local IP address and it is working fine now.我用我的本地 IP 地址替换了主机,现在它工作正常。
Put this in a try/ catch block and see if it can catch any exceptions把它放在一个 try/catch 块中,看看它是否可以捕获任何异常
final QueryResult result = await _instance._client.mutate(mutationOptions);
the original question was how to get the graphql errors.最初的问题是如何获得 graphql 错误。 I'm using graphql: ^5.0.0
我正在使用 graphql: ^5.0.0
I checked the docs and found this example:我检查了文档并找到了这个例子:
if (result.hasException) {
if (result.exception.linkException is NetworkException) {
// handle network issues, maybe
}
return Text(result.exception.toString())
}
but that just gave me the exception, not the error, I casted the result exception to the type of error and was able to get the message:但这只是给了我异常,而不是错误,我将结果异常转换为错误类型并能够得到消息:
if (result.hasException) {
if (result.exception!.linkException is ServerException) {
ServerException exception =
result.exception!.linkException as ServerException;
var errorMessage = exception.parsedResponse!.errors![0].message;
print(errorMessage);
throw Exception(errorMessage);
}
}
this seems like a lot of work for a simple message, I wonder if there is another simpler built-in way to do it对于一个简单的消息来说,这似乎需要做很多工作,我想知道是否还有另一种更简单的内置方法可以做到这一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.