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,
})
override

query allow made GraphQL raw queries final connect = GetConnect(); 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, {
      'query': query,
      'variables': variables,
    }, headers: headers);

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