0

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?

Martin Cup
  • 2,399
  • 1
  • 21
  • 32

2 Answers2

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'
    },
  • 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