query method
Execute a GraphQL query with optional variables; returns the data
object. Throws GraphQLException if the response carries errors.
Implementation
Future<Map<String, dynamic>> query(
String query, {
Map<String, dynamic>? variables,
}) async {
final resp = await _dio.post<Map<String, dynamic>>(
endpoint,
data: {
'query': query,
if (variables != null) 'variables': variables,
},
options: Options(
contentType: 'application/json',
responseType: ResponseType.json,
headers: {'accept': 'application/json', ...headers},
validateStatus: (_) => true,
),
);
final body = resp.data;
if (body == null) {
throw GraphQLException(
'Empty GraphQL response (HTTP ${resp.statusCode})',
const [],
);
}
final errors = body['errors'];
if (errors is List && errors.isNotEmpty) {
final messages =
errors.map((e) => (e is Map ? e['message'] : e).toString()).toList();
throw GraphQLException(messages.join('; '), messages);
}
final data = body['data'];
if (data is! Map<String, dynamic>) {
throw GraphQLException('GraphQL response missing "data"', const []);
}
return data;
}