5

I am using the package graphql_flutter for GraphQL operations in my flutter app. 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.

Anik
  • 2,692
  • 2
  • 22
  • 25

3 Answers3

4

I found the problem. It is because android cannot connect to 127.0.0.1 or localhost from the emulator. I replaced the host with my local IP address and it is working fine now.

Anik
  • 2,692
  • 2
  • 22
  • 25
0

Put this in a try/ catch block and see if it can catch any exceptions

final QueryResult result = await _instance._client.mutate(mutationOptions);
  • after that line i am able to print result.exception so obviously there is no exception coming from the _client.mutate call. It is already handled inside of that function. – Anik Jun 22 '20 at 08:34
  • Then the error should be inside the result data instead of result exception. The exception is only for syntax and network-related issues. Not the exception thrown by the middleware. – Saiful Islam Adar Jun 22 '20 at 09:17
  • You should check these errors manually, like result.data["errors"] != null or somethng – Saiful Islam Adar Jun 22 '20 at 09:18
  • result.data prints null, and " The exception is only for syntax and network-related issues" - DIdn't find that anywhere in the documentation, I did the way the documentation mentions. – Anik Jun 22 '20 at 13:45
  • It should not be. GraphQL returns status code 200 for all the results. It's a thing of the GraphQL server. You will have to manually check if the result is actual data or errors. – Saiful Islam Adar Jun 23 '20 at 09:46
0

the original question was how to get the graphql errors. I'm using 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

Rootdevelopper
  • 119
  • 1
  • 3