updateUserAvatar method
- MultipartFile? file,
- CropAreaInput? crop, {
- void onSendProgress()?,
Updates or resets the MyUser.avatar field with the provided image
file.
HTTP request for this mutation must be Content-Type: multipart/form-data
containing the uploaded file and the file argument itself must be null,
otherwise this mutation will fail.
Authentication
Mandatory.
Result
One of the following MyUserEvents may be produced on success:
- EventUserAvatarUpdated (if image
fileis provided); - EventUserAvatarRemoved (if image
fileis not provided).
Idempotent
Succeeds as no-op (and returns no MyUserEvent) if the authenticated
MyUser uses the specified image file already as his avatar with the
same crop area.
Implementation
Future<MyUserEventsVersionedMixin?> updateUserAvatar(
dio.MultipartFile? file,
CropAreaInput? crop, {
void Function(int count, int total)? onSendProgress,
}) async {
Log.debug('updateUserAvatar($file, $crop, onSendProgress)', '$runtimeType');
final variables = UpdateUserAvatarArguments(file: null, crop: crop);
final query = MutationOptions(
operationName: 'UpdateUserAvatar',
document: UpdateUserAvatarMutation(variables: variables).document,
variables: variables.toJson(),
);
final request = query.asRequest;
final body = const RequestSerializer().serializeRequest(request);
final encodedBody = json.encode(body);
try {
final response = await client.post(
file == null
? encodedBody
: dio.FormData.fromMap({
'operations': encodedBody,
'map': '{ "file": ["variables.upload"] }',
'file': file,
}),
options: file == null
? null
: dio.Options(contentType: 'multipart/form-data'),
operationName: query.operationName,
onSendProgress: onSendProgress,
onException: (data) => UpdateUserAvatarException(
(UpdateUserAvatar$Mutation.fromJson(data).updateUserAvatar
as UpdateUserAvatar$Mutation$UpdateUserAvatar$UpdateUserAvatarError)
.code,
),
);
if (response.data['data'] == null) {
throw GraphQlException([
GraphQLError(message: response.data.toString()),
]);
}
return (UpdateUserAvatar$Mutation.fromJson(
response.data['data'],
).updateUserAvatar
as MyUserEventsVersionedMixin?);
} on dio.DioException catch (e) {
if (e.response?.statusCode == 413) {
throw const UpdateUserAvatarException(
UpdateUserAvatarErrorCode.invalidSize,
);
}
rethrow;
}
}