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

File not uploading properly #296

Closed sagarPakhrin closed 2 years ago

sagarPakhrin commented 2 years ago

ISSUE

while uploading file with rest, the type of received file is

{
  fieldname: 'file',
  originalname: 'Ellipse 41.png',
  encoding: '7bit',
  mimetype: 'image/png',
  buffer: <Buffer 89  73 00 00 2c 4b 00 00 2c 4b 01 ... 44542 more bytes>,
  size: 44592
}

But with graphql-upload, the received file is in the format

 {
   filename: 'Ellipse 41.png',
   mimetype: 'image/png',
   encoding: '7bit',
   createReadStream: [Function: createReadStream]
}

When I try to create a stream from the file.createReadStream() I get the following error

(node:113) [DEP0135] DeprecationWarning: ReadStream.prototype.open() is deprecated
     at ReadStream.open (/home/node/app/node_modules/fs-capacitor/lib/index.js:90:11)
     at WriteStream.<anonymous> (/home/node/app/node_modules/fs-capacitor/lib/index.js:84:49)
     at Object.onceWrapper (node:events:510:26)
     at WriteStream.emit (node:events:402:35)
     at WriteStream.stream.emit (node:internal/fs/streams:56:9)
     at /home/node/app/node_modules/fs-capacitor/lib/index.js:143:14
     at FSReqCallback.oncomplete (node:fs:188:23)

So the file uploaded to gcs/s3 or local file system doesn't get written correctly because it's still a stream and not a buffer it seems like the issue is with fc-capacitor package

jaydenseric commented 2 years ago

What exactly is your GraphQL server environment; Node.js version, etc.

sagarPakhrin commented 2 years ago

What exactly is your GraphQL server environment; Node.js version, etc.

Im in node 14, I tried in node 16 as well same issue.

I'm using graphql-upload with nestjs

jaydenseric commented 2 years ago

You didn't fully answer the question.

As I was writing this comment I see in your edit you mention nestjs, but please provide the other details.

sagarPakhrin commented 2 years ago

My machine has node v14.19.0 but my code runs in a docker container from node:16 base image. for graphql server I'm using these: @nestjs/graphql @nestjs/apollo graphql apollo-server-express

graphql-upload version: ^13.0.0",

Looks like graphql-upload is using fs-capacitor@6.2.0

I also get this when I do npm ls fs-capacitor

+-- apollo-server-express@2.25.3 invalid: "^3.1.2" from node_modules/@nestjs/graphql
| `-- apollo-server-core@2.25.3
|   `-- @apollographql/graphql-upload-8-fork@8.1.3
|     `-- fs-capacitor@2.0.4
`-- graphql-upload@13.0.0
  `-- fs-capacitor@6.2.0

So I guess there are multiple versions of fs-capacitor

jaydenseric commented 2 years ago

Apollo Server v2 ships with an outdated file uploads implementation built in, which conflicts with the modern manual graphql-upload setup. Instead of disabling the outdated implementation in Apollo Server v2, you're probably better off figuring out how to use Apollo Server v3 with NestJS.