query<T> method

  1. @override
Future<GraphQLResponse<T>> query<T>(
  1. String query, {
  2. String? url,
  3. Map<String, dynamic>? variables,
  4. Map<String, String>? headers,
})
inherited

query allow made GraphQL raw querys final connect = ExConnect(); connect.baseUrl = 'https://countries.trevorblades.com/'; final response = await connect.query( r""" { country(code: "BR") { name native currency languages { code name } } } """, ); print(response.body);

Implementation

@override
Future<GraphQLResponse<T>> query<T>(
  String query, {
  String? url,
  Map<String, dynamic>? variables,
  Map<String, String>? headers,
}) async {
  try {
    final res = await post(
      url ?? '',
      body: {'query': query, 'variables': variables},
      header: headers,
    );

    final listError = res.body['errors'];
    if ((listError is List) && listError.isNotEmpty) {
      return GraphQLResponse<T>(
        graphQLErrors: listError
            .map(
              (e) => GraphQLError(
                code: e['extensions']['code']?.toString(),
                message: e['message']?.toString(),
              ),
            )
            .toList(),
      );
    }
    return GraphQLResponse<T>.fromResponse(res);
  } on Exception catch (_) {
    return GraphQLResponse<T>(
      graphQLErrors: [GraphQLError(message: _.toString())],
    );
  }
}