jaydenseric / graphql-upload

Middleware and a scalar Upload 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 132 forks source link

Multiple File Uploads with express-graphql #202

Closed victorpowah closed 4 years ago

victorpowah commented 4 years ago

Hi, I have followed the steps to upload multiple files but somehow express-graphql can´t read the files one after the other. I am uploading the files following the [standard].(https://github.com/jaydenseric/graphql-multipart-request-spec)

The code I have is the next one:

uploadFeedImages = async function (upload, context) {
    let { filename, mimetype, encoding, createReadStream } = await upload;
    let stream = createReadStream();

    var chunks = []
    var result = await new Promise((resolve, reject) => {
        stream.on('data', chunk => chunks.push(chunk))
        stream.on('error', reject)
        stream.on('end', () => resolve(Buffer.concat(chunks)))
    })

    return { filename, mimetype, encoding };
}

The error I get is: createReadStream is not a function.

jaydenseric commented 4 years ago

Try running npm ls graphql-upload to make sure your using the latest version, maybe the version installed predates the createReadStream API.

victorpowah commented 4 years ago

The version im using is the latest one. -- graphql-upload@10.0.0

jaydenseric commented 4 years ago

If there is only one version installed, and it's the latest, then perhaps upload being passed into your uploadFeedImages function is something unexpected - not an upload. Debugging your project is not something I can really help with, sorry.

victorpowah commented 4 years ago

That´s what i get on the upload attribute

{ pictures: [ Upload { resolve: [Function], reject: [Function], promise: [Promise], file: [Object] }, Upload { resolve: [Function], reject: [Function], promise: [Promise] } ] }

It still cant parse it and its not a problem about the project, this is what express-graphql attach to the upload variable.

jaydenseric commented 4 years ago

Maybe you are not using the Upload scalar in your schema.

victorpowah commented 4 years ago

Is there any problem using the buildSchema function of express-graphql?

The mutation is:

uploadFeedImages(
            pictures: [Upload!]!
        ): [Image]

And yes, we have the scalar Upload in the schema.

jaydenseric commented 4 years ago

Probably the Upload scalar isn't implemented using GraphQLUpload.

I highly recommend using JS to create a schema, instead of having a seperate SDL string and resolvers you then have to try to bring together.

This is an old, but relevant Stack Overflow question: https://stackoverflow.com/questions/47824603/graphql-custom-scalar-definition-without-graphql-tools

mike-marcacci commented 4 years ago

The upload argument according to your post is of the following shape:

type YourUploadArg = {
  pictures: Upload[]
}

I think you mean to iterate through the uploads in the pictures property...

SergioSuarezDev commented 2 years ago

Similar to mine: https://github.com/jaydenseric/graphql-upload/issues/276