0

This is the error that pops up for me.

In the above error, I tried to get the parsedResponse and add handling for the statusCode, but I couldn't catch the error.

The linkException doesn't seem to recognize the parsedResponse. If you know a solution, please let me know.

thank you

Below is the graphql client I made and use using graphql_flutter

import 'package:firebase_auth/firebase_auth.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
// ignore: implementation_imports
import 'package:gql/src/ast/ast.dart';
import 'package:noling_app/config/parser.dart';

class MyGraphQLClient {
  late GraphQLClient _client;
  GraphQLClient get client => _client;

  GraphQLClient setClient(String idToken) {
    HttpLink _httpLink = HttpLink(
      'http://localhost:5005/noling-develop/asia-northeast3/graphql',
      parser: CustomResponseParser(),
      defaultHeaders: {
        'X-USER-TOKEN': idToken,
      },
    );
    Link _link;
    if (idToken != '' && idToken.isNotEmpty) {
      final AuthLink authLink = AuthLink(
        getToken: () => idToken,
        headerKey: 'X-USER-TOKEN',
      );
      _link = authLink.concat(_httpLink);
    } else {
      _link = _httpLink;
    }

    _client = GraphQLClient(
      cache: GraphQLCache(),
      link: _link,
    );

    return _client;
  }

  Future<dynamic> query(
    DocumentNode document, {
    Map<String, dynamic>? data,
  }) async {
    try {
      QueryResult result = await _client.query(QueryOptions(
        document: document,
        variables: data ?? {},
      ));

      if (result.hasException) {
        print(result);
        var message = result.exception!.graphqlErrors.first.message;
        throw Exception(message);
      }

      return result.data;
    } catch (e) {
      print("error catch ?");
      rethrow;
    }
  }

  Future<dynamic> mutate(
    DocumentNode document, {
    Map<String, dynamic>? data,
  }) async {
    var result = await _client.mutate(MutationOptions(
      document: document,
      variables: data ?? {},
    ));

    if (result.hasException) {
      var message = result.exception!.graphqlErrors.first.message;
      throw GraphQLError(message: message);
    }

    return result.data;
  }
}

MyGraphQLClient graphQLClient = MyGraphQLClient();

In the MyGraphQLClient class, I created and used a CustomResponseParser like the one I saw in another issue post, but to no avail.

class CustomResponseParser extends ResponseParser {
  @override
  Response parseResponse(Map<String, dynamic> body) {
    Map<String, String> errors = new Map();
    if (body["errors"] != null) {
      errors['message'] = body["errors"][0];
    }
    Response res = Response(
      response: body,
      errors: (body["errors"] as List?)
          ?.map(
            (dynamic error) => parseError(errors),
          )
          .toList(),
      data: body["data"] as Map<String, dynamic>?,
      context: const Context().withEntry(
        ResponseExtensions(
          body["extensions"],
        ),
      ),
    );

    print(res);

    return res;
  }

  @override
  GraphQLError parseError(Map<String, dynamic> error) {
    return GraphQLError(
      message: error['message'],
    );
  }
}

1 Answers1

1

It looks like you can access parsedResponse if you cast result.exception.linkException as a ServerException first.

Example:

if(result.exception?.linkException != null)
  String? errorMessage = (result.exception!.linkException as ServerException).parsedResponse?.errors?.first.message;

Credit goes to the solution found here (link edited): Unable to retrieve errors occured in Graphql mutation in flutter project

-- Thanks @DavidW for the link correction

mushi
  • 11
  • 3
  • 1
    https://stackoverflow.com/q/62501369/ is the original link. The website you found is a scraper site that copies content from stack overflow – DavidW May 12 '23 at 19:22