Upload file with apollo-upload-client and graphql-yoga 3.x

465 views Asked by At

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?

2

There are 2 answers

0
Martin Cup On

The easy answer is, that it just works by using Upload instead of File in the query.

1
productdevbook On

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