In the 3.x versions of graphql-yoga fileuploads use the scalar type File for queries, but apollo-upload-client uses Upload, so how can I make it work with those frameworks?
Asked
Active
Viewed 322 times
2 Answers
0
The easy answer is, that it just works by using Upload instead of File in the query.

Martin Cup
- 2,399
- 1
- 21
- 32
0
This is off topic, but you can make a simpler solution by just sending a File. You need to remove apollo-upload-client from the list. Also on the backend. Pure file upload example.
shema.graphql
scalar File
extend type Mutation {
profileImageUpload(file: File!): String!
}
resolver.ts
profileImageUpload: async (_, { file }: { file: File }) => {
const _file = await file.arrayBuffer()
if (_file) {
const image = sharp(_file)
const metadata = await image.metadata()
console.log(metadata, 'metadata')
try {
const image = await sharp(_file).resize(600, 600).webp().toBuffer()
fs.writeFileSync('test.webp', image)
console.log(image, 'image')
}
catch (error) {
console.error(error)
}
}
return 'a'
},

productdevbook
- 21
- 1
- 3
-
I think that code can be simplified as converting `ArrayBuffer` to Node's `Buffer`. But please notice that `Buffer` and `sharp` are Node specific libraries while Yoga isn't; `const _file = Buffer.from(await file.arrayBuffer());` So basically you don't need to consume it as a stream. – ardatan Jan 25 '23 at 14:25