📅  最后修改于: 2022-03-11 14:57:26.730000             🧑  作者: Mango
import * as FileType from 'file-type'
import { GraphQLError, GraphQLScalarType } from 'graphql'
import { Readable } from 'stream'
export interface FileUpload {
filename: string
mimetype: string
encoding: string
createReadStream: () => Readable
}
export const GraphQLUpload = new GraphQLScalarType({
name: 'Upload',
description: 'The `Upload` scalar type represents a file upload.',
async parseValue(value: Promise): Promise {
const upload = await value
const stream = upload.createReadStream()
const fileType = await FileType.fromStream(stream)
if (fileType?.mime !== upload.mimetype)
throw new GraphQLError('Mime type does not match file content.')
return upload
},
parseLiteral(ast): void {
throw new GraphQLError('Upload literal unsupported.', ast)
},
serialize(): void {
throw new GraphQLError('Upload serialization unsupported.')
},
})