[英]Trying to upload a file with Ferry GraphQL and Flutter throws a "Converting object to an encodable object failed: Instance of 'MultipartFile'". Error
I've been following this guide https://medium.com/@p02diada/how-to-upload-files-with-ferry-graphql-and-flutter-34a2801d6a8a on how to upload a file using Ferry GraphQL. The only difference is that I'm using Dio's MultipartFile instead of http's MultipartFile.我一直在关注本指南https://medium.com/@p02diada/how-to-upload-files-with-ferry-graphql-and-flutter-34a2801d6a8a ,了解如何使用 Ferry GraphQL 上传文件。唯一的区别是我使用的是 Dio 的 MultipartFile 而不是 http 的 MultipartFile。 Whenever I try to call the mutation to upload, I'm always thrown a
每当我尝试调用突变进行上传时,我总是会抛出一个
`GqlException(LinkException(DioError [DioErrorType.other]: Converting object to an encodable object failed: Instance of 'MultipartFile`` error. `GqlException(LinkException(DioError [DioErrorType.other]:将 object 转换为可编码的 object 失败:“MultipartFile”错误的实例。
GraphQL file..
mutation UploadDocumentMutation($input: UploadDocumentInput!, $file: Upload!) {
uploadDocument(input: $input, file: $file) {
onboardingId,
}
}
build.yaml file..
gql_build|schema_builder:
enabled: true
options:
schema: schemaLocation
type_overrides:
Upload:
name: MultipartFile
import: 'package:dio/dio.dart'
gql_build|ast_builder:
enabled: true
gql_build|data_builder:
enabled: true
options:
schema: schemaLocation
type_overrides:
Upload:
name: MultipartFile
import: 'package:dio/dio.dart'
gql_build|var_builder:
enabled: true
options:
schema: schemaLocation
type_overrides:
Upload:
name: MultipartFile
import: 'package:dio/dio.dart'
gql_build|serializer_builder:
enabled: true
options:
schema: schemaLocation
custom_serializers:
- import: 'package:path_to/upload_serializer.dart'
name: UploadSerializer
ferry_generator|req_builder:
enabled: true
options:
schema: schemaLocation
type_overrides:
Upload:
name: MultipartFile
import: 'package:dio/dio.dart'
upload_serializer file..
import 'package:dio/dio.dart' show MultipartFile;
class UploadSerializer extends PrimitiveSerializer<MultipartFile> {
@override
MultipartFile deserialize(
Serializers serializers,
Object serialized, {
FullType specifiedType = FullType.unspecified,
}) {
assert(serialized is List<int>, "FileSerializer expected 'Uint8List' but got ${serialized.runtimeType}");
return MultipartFile.fromBytes(serialized as List<int>);
}
@override
Object serialize(
Serializers serializers,
MultipartFile file, {
FullType specifiedType = FullType.unspecified,
}) =>
file;
@override
Iterable<Type> get types => [MultipartFile];
@override
String get wireName => "Upload";
}
my upload method..
Future<GUploadDocumentMutationData_uploadDocument?> uploadDocument(
UploadDocumentInput uploadDocumentInput,
MultipartFile multipartFile,
) async {
final input = GUploadDocumentInputBuilder()
..onboardingId = uploadDocumentInput.onboardingId
..type = uploadDocumentInput.type.gType
..side = uploadDocumentInput.side.gType;
final GUploadDocumentMutationReq request = GUploadDocumentMutationReq(
(b) => b
..vars.input.onboardingId = input.onboardingId
..vars.input.type = input.type
..vars.input.side = input.side
..vars.file = multipartFile,
);
try {
final OperationResponse<GUploadDocumentMutationData, GUploadDocumentMutationVars> response =
await client.request(request).first;
final GUploadDocumentMutationData? responseData = processResponse(response);
if (responseData != null) {
return responseData.uploadDocument;
} else {
return null;
}
}
My multipart file object also comes from this我的多部分文件 object 也来自于此
final multipartFile = await MultipartFile.fromFile(
Xfile.path,
);
Sorry for all of the code, I felt like a lot of it was necessary.对不起所有的代码,我觉得很多都是必要的。 Thank you.
谢谢你。
Did you try the same fix the article author used to resolve this problem?您是否尝试过文章作者用来解决此问题的相同修复方法?
The article recommends to simply not cache the result from the multipart queries like so:文章建议不要像这样缓存多部分查询的结果:
final req = GUploadImageReq(
(b) => b
..vars.input = input
..fetchPolicy = FetchPolicy.NoCache,
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.