jaydenseric / graphql-upload

Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 131 forks source link

BadRequestError: Misordered multipart fields; ‘map’ should follow ‘operations’ #257

Closed Akifcan closed 3 years ago

Akifcan commented 3 years ago

Hello i tried upload file with graphql-upload package but i have this error.

BadRequestError: Misordered multipart fields; ‘map’ should follow ‘operations’ (https://github.com/jaydenseric/graphql-multipart-request-spec).
    at Busboy.<anonymous> (C:\Users\user\Desktop\grap\node_modules\graphql-upload\public\processRequest.js:175:17)

i found your solution on stackoverflow (https://stackoverflow.com/questions/62569041/graphql-upload-getting-invalid-json-in-the-operations-multipart-field) and tried but still doesn't work

this is my client request

       document.querySelector('form').addEventListener('submit', e => {
                e.preventDefault()
                const file = document.querySelector('input').files[0]
                const formData = new FormData()
                formData.append("query", "{ 'query': 'mutation($file: Upload!) { uploadFile(file: $file) }', 'variables': { 'file': null } }")
                formData.append("map", "{ 'nfile': ['variables.file'] }")
                formData.append("nfile", file)
                fetch('http://localhost:3000/graphql', {
                    method: 'POST',
                    body: formData
                })
                    .then(r => r.json())
                    .then(data => console.log('data returned:', data));

            })

and backend

  @Mutation(() => Boolean)
    async uploadFile(@Args({name: 'file', type: () => GraphQLUpload})
    {
        createReadStream,
        filename
    }: FileUpload): Promise<boolean> {
            return new Promise(async (resolve, reject) => 
                createReadStream()
                    .pipe(createWriteStream(`./uploads/${filename}`))
                    .on('finish', () => resolve(true))
                    .on('error', () => reject(false))
                    );
    }
jaydenseric commented 3 years ago

There are a few errors: