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

Cannot write file because it awaits indefinitely #321

Closed Atlinx closed 2 years ago

Atlinx commented 2 years ago

I'm following Apollo's tutorial on integrating graphql-upload, except I'm using Angular as my front end. I have apollo-angular and apollo-upload-client installed on my front-end, and graphql-upload installed on my backend.

I'm able to receive the file, however I cannot write the file anywhere. My resolver ends up awaiting indefinitely. Here's my resolver code:

export default {
  Mutation: {
    updateUser: async (parent, args, context: ApolloResolversContext, info) => {
      if (args.input) {
        const { createReadStream, filename, mimetype, encoding } = await <FileUpload>args.input;

        console.log('file uploading...');

        // Invoking the `createReadStream` will return a Readable Stream.
        // See https://nodejs.org/api/stream.html#stream_readable_streams
        const stream = createReadStream();

        // This is purely for demonstration purposes and will overwrite the
        // local-file-output.txt in the current working directory on EACH upload.
        const out = fs.createWriteStream(`uploads/${filename}`);
        stream.pipe(out);
        await finished(out);

        console.log('file uploaded!');
      }

      return true;
    }
  }
} as { Query: QueryResolvers, Mutation: MutationResolvers };

Due to dependency issues, I'm also using graphql v15.8.0, which means I'm relying on an older version of graphql-upload (v13.0.0).

Atlinx commented 2 years ago

I found the issue. The path should be relative to the root directory instead of my source directory. Changing the write stream to src/uploads/${filename} fixed it.