mutation<T> method
Implementation
@override
Future<GraphQLResponse<T>> mutation<T>(
String mutation, {
String? url,
Map<String, dynamic>? variables,
Map<String, String>? headers,
}) async {
try {
// Use empty string if url is null to use baseUrl
final targetUrl = url ?? '';
final res = await post(
targetUrl,
{
'query': mutation,
'variables': variables,
},
headers: headers);
// Check if body is null or not a Map
final body = res.body;
if (body == null) {
return GraphQLResponse<T>(
graphQLErrors: [
GraphQLError(
code: 'NULL_RESPONSE',
message: res.statusText ?? 'Server returned null response',
),
],
);
}
// Handle case where body is not a Map
if (body is! Map) {
return GraphQLResponse<T>.fromResponse(res);
}
final listError = body['errors'];
if ((listError is List) && listError.isNotEmpty) {
return GraphQLResponse<T>(
graphQLErrors: listError
.map(
(e) => GraphQLError(
code: (e is Map && e['extensions'] != null)
? e['extensions']['code']?.toString()
: null,
message: (e is Map) ? e['message']?.toString() : null,
),
)
.toList(),
);
}
return GraphQLResponse<T>.fromResponse(res);
} on Exception catch (err) {
return GraphQLResponse<T>(
graphQLErrors: [GraphQLError(code: null, message: err.toString())],
);
}
}