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

File stream is null #135

Closed prajjwaldimri closed 5 years ago

prajjwaldimri commented 5 years ago

For some reason, my server is returning an empty stream instead of the file.

Here's the client's request:

-----------------------------81678640518046430391178682890
Content-Disposition: form-data; name="operations"

{"operationName":null,"variables":{"file":null},"query":"mutation ($file: Upload!) {\n  uploadUserImage(file: $file)\n}\n"}
-----------------------------81678640518046430391178682890
Content-Disposition: form-data; name="map"

{"1":["variables.file"]}
-----------------------------81678640518046430391178682890
Content-Disposition: form-data; name="1"; filename="signature.jpg"
Content-Type: image/jpeg

******** Whole Lot of data ************
-----------------------------81678640518046430391178682890--

On the server

async (parent, { file }) => {  
  const { createReadStream, filename, mimetype, encoding } = await file;
  const stream = createReadStream();

  stream.on("data", data => {
      console.log("DATA**********************");
      console.log(data);      
    });

  stream.on("readable", () => {
      console.log(stream.read());
    });
}

Nothing gets outputted. When I send stream's data as response it returns as null.

Interestingly, filename, mimetype and encoding all are populated.

I am using apollo-server-express as my server.

Here's the schema for the method

uploadUserImage(file: Upload!): String

Will really appreciate your help on this. Thanks :)

jaydenseric commented 5 years ago

You should be promisifying and awaiting the file upload like in the example:

https://github.com/jaydenseric/apollo-upload-examples/blob/master/api/resolvers.mjs#L21

Give that a go and see if you still have the issue.

prajjwaldimri commented 5 years ago

Whoa! It worked. But I still don't understand what's going on here. I mean aren't the readableStreams supposed to be read from events? Why are those events outputting null?

jaydenseric commented 5 years ago

What was happening, was the resolver was instantly returning without waiting for the file to finish uploading. When all the resolvers return, the GraphQL server sends a response. What happens if the server responds before the client has finished uploading the request? It disconnects.

prajjwaldimri commented 5 years ago

Got it. Thanks :+1: :)

bublig737 commented 4 years ago

You should be promisifying and awaiting the file upload like in the example:

https://github.com/jaydenseric/apollo-upload-examples/blob/master/api/resolvers.mjs#L21

Give that a go and see if you still have the issue.

@jaydenseric this page is not found. Would you update this information please? I has the same problem.

jaydenseric commented 4 years ago

@marusapur see:

https://github.com/jaydenseric/apollo-upload-examples/blob/cef7077c09178022f9b1c4e2425dbf46b0b6dd0a/api/server.js#L35

hshankar-loop commented 2 years ago

Can someone help to solve the same in python using Django? Thanks in advance!