0

Im trying to upload file from react application , there are 3arguments: filename, mimetype and encoding . After the file is uploaded all three are passed to the server but there is no parameter createReadStream. $ node -v v16.13.2

I've tried so far to send the file to the apollo-server.

my Queries are following:

export const UPLOAD = gql`
  mutation Upload($uploadFile: Upload) {
    fileUpload {
      uploadFile(file: $uploadFile) {
        filename
        mimetype
        encoding
      }
    }
  }
`;
import { useMutation } from '@apollo/client'
import React from 'react'
import { UPLOAD  } from './queries/Queries'
const UploadCV = () => {

    const  [UploadCV, { data, error,refe }]= useMutation(UPLOAD,{
      context:{clientName:'upload'},
        onComplete:data=>console.log(data)
    })

    const handleUpload = (e)=>{
      console.log(e.target.files[0])
      const file = e.target.files[0]
        // console.log(typeof file.name)
        // console.log(typeof file.type)
        // console.log(typeof String(file.size))
        if(!file)return
      UploadCV({
          variables:{
            uploadFile: {
              filename: file.name,
              mimetype: file.type,
              encoding: String(file.size),
            }
          }
      })
    }
  return (
    <div>
     <input type="file"
       onChange={handleUpload}
    
          id="avatar" name="avatar"
       accept="image/png, image/jpeg, application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
       />
    </div>
  )
}

export default UploadCV

enter image description here

Till now its fine but once its hit server start the problem.

const Upload = require("graphql-upload-minimal").GraphQLUpload



module.exports = {
  fileUpload: {
    Upload,
    async uploadFile({ file:{ filename,mimetype, encoding,createReadStream} }) {
        console.log('FileName:',filename , 'MiemeType:', mimetype,'Encoding:', encoding,'CreateReadStream:',createReadStream)

`
    },
  },
};

That is the output of the sended file. enter image description here

My schema

const { buildSchema } = require('graphql');
const uploadFile = require('./uploadFile');

module.exports = buildSchema(`
    scalar Upload
    ${uploadFile}

    type Mutation{
        fileUpload:UploadFile
    }
    type Query{
        getCVs:getAllCV
    }
`);
module.exports = `
  

    type File{
        filename:String
        mimetype:String
        encoding:String
    }

    type UploadFile{
        uploadFile(file:Upload):File
    }

    type getAllCV{
        getAllCV:[File]
}`

0 Answers0