[当我将文件上传到graphql时,我注意到,有时我将它们作为Promise获得,有时我会立即获得已解析的文件。经过一些测试,我发现,如果我将文件上传为直接Arg,则文件会自动解析,但是如果嵌套在对象中,则会收到Promise(但其中的文件已经存在),您必须等待使用。为了清楚起见,我将添加一个小的代码示例来说明:
@InputType()
export class TestFileType {
@Field(type => GraphQLUpload)
file: IUpload;
}
interface IUpload {
filename: string;
mimetype: string;
encoding: string;
createReadStream: () => Stream;
}
@Mutation(of => Boolean)
async fileUploadTest(
@Arg('file', () => GraphQLUpload) file: IUpload,
@Arg('fileObj', type => TestFileType) fileObj: TestFileType,
): Promise<boolean> {
console.log('Test File: ', file);
/* GIVES
Test File: {
filename: 'test.png',
mimetype: 'image/png',
encoding: '7bit',
createReadStream: [Function: createReadStream]
}
*/
console.log('Test OBJ file: ', fileObj.file);
/* GIVES
Test OBJ file: Promise {
{
filename: 'test.png',
mimetype: 'image/png',
encoding: '7bit',
createReadStream: [Function: createReadStream]
}
}
*/
return true;
}
在两种情况下,我都希望收到已解决的文件或Promise。我应该将其报告给问题跟踪器还是丢失了?
可以在GraphQLSchema中使用的GraphQL上传标量。解析程序的价值在于可以解决文件上传详细信息以进行处理和存储的承诺。
因此,您应该报告收到的非承诺值不是“ thenable”。