uploadAttachment method

Future<UploadAttachment$Mutation$UploadAttachment$UploadAttachmentOk> uploadAttachment(
  1. MultipartFile? attachment, {
  2. void onSendProgress(
    1. int count,
    2. int total
    )?,
})

Creates a new Attachment linked to the authenticated MyUser for a later use in the postChatMessage mutation.

HTTP request for this mutation must be Content-Type: multipart/form-data containing the uploaded file and the attachment argument must be null, otherwise this mutation will fail.

Authentication

Mandatory.

Non-idempotent

Each time creates a new unique Attachment.

Implementation

Future<UploadAttachment$Mutation$UploadAttachment$UploadAttachmentOk>
uploadAttachment(
  dio.MultipartFile? attachment, {
  void Function(int count, int total)? onSendProgress,
}) async {
  Log.debug('uploadAttachment($attachment, onSendProgress)', '$runtimeType');

  final variables = UploadAttachmentArguments(file: null);
  final query = MutationOptions(
    operationName: 'UploadAttachment',
    document: UploadAttachmentMutation(variables: variables).document,
    variables: variables.toJson(),
  );

  final request = query.asRequest;
  final body = const RequestSerializer().serializeRequest(request);
  final encodedBody = json.encode(body);

  try {
    var response = await client.post(
      dio.FormData.fromMap({
        'operations': encodedBody,
        'map': '{ "file": ["variables.upload"] }',
        'file': attachment,
      }),
      options: dio.Options(contentType: 'multipart/form-data'),
      operationName: query.operationName,
      onSendProgress: onSendProgress,
      onException:
          (data) => UploadAttachmentException(
            (UploadAttachment$Mutation.fromJson(data).uploadAttachment
                    as UploadAttachment$Mutation$UploadAttachment$UploadAttachmentError)
                .code,
          ),
    );

    if (response.data['data'] == null) {
      throw GraphQlException([
        GraphQLError(message: response.data.toString()),
      ]);
    }

    return (UploadAttachment$Mutation.fromJson(
          response.data['data'],
        )).uploadAttachment
        as UploadAttachment$Mutation$UploadAttachment$UploadAttachmentOk;
  } on dio.DioException catch (e) {
    if (e.response?.statusCode == 413) {
      throw const UploadAttachmentException(
        UploadAttachmentErrorCode.invalidSize,
      );
    }

    Log.error('Failed to upload attachment: ${e.response}', '$runtimeType');

    rethrow;
  }
}