简体   繁体   English

使用 typegraphql (Apollo Server) 上传文件

[英]File uploads with typegraphql (Apollo Server)

What I am trying to do我想做什么

I am trying to get file uploads working with typegraphql (wrapper on Apollo Server).我正在尝试使用typegraphql (Apollo Server 上的包装器)进行文件上传。 I am writing a simple resolver that should receive a file upload and write it to a file on the server.我正在编写一个简单的解析器,它应该接收文件上传并将其写入服务器上的文件。

My code我的代码

I followed this tutorial我跟着这个教程

I have uploaded a very small demo of the problem to this github repo .我已经将这个问题的一个非常小的演示上传到这个github repo And here is my resolver for receiving the uploaded file这是我接收上传文件的解析器

@Resolver()
export class ProfilePictureResolver {
  @Mutation(() => Boolean)
  async addProfilePicture(
    @Arg('picture', () => GraphQLUpload)
    upload: FileUpload
  ): Promise<boolean> {
    console.log('upload', upload);
    const { createReadStream, filename } = upload;
    return new Promise(async (resolve, reject) =>
      createReadStream()
        .pipe(createWriteStream(__dirname + `/../../../images/${filename}`))
        .on('finish', () => resolve(true))
        .on('close', () => resolve(true))
        .on('error', () => reject(false))
    );
  }
}

My Problem我的问题

When I run this resolver (using postman as shown in the tutorial) the promise never resolves.当我运行这个解析器(使用教程中所示的邮递员)时,承诺永远不会解决。 createReadStream doesn't emit a close or error or finish event. createReadStream不会发出关闭或错误或完成事件。 The image file does get created but it is empty (0 bytes).图像文件确实被创建,但它是空的(0 字节)。

Any help would be greatly appreciated.任何帮助将不胜感激。

Thanks谢谢

I make this in a different way maybe this can help you I use graphql-upload library to upload an image我用不同的方式做这个,也许这可以帮助你我使用graphql-upload库上传图像

in your index or server file, you need to have this, in my case I do it whit express, but there is a setup whit apollo在您的索引或服务器文件中,您需要有这个,在我的情况下,我是这样做的,但是有一个设置 whit apollo

import { graphqlUploadExpress } from 'graphql-upload'
import { graphqlHTTP }  from 'express-graphql';

    App.use('/graphql', graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }), graphqlHTTP(async(req, res) => 
    ({
        schema: await buildSchema
        ({
            resolvers: 
            [
                ProfilePictureResolver ,
            ],

        }),


    })));

And the resolver和解析器

@Resolver()
export class ProfilePictureResolver 
{
  @Mutation(() => Boolean)
  async addProfilePicture(
  @Arg('picture', () => GraphQLUpload)upload: FileUpload): Promise<boolean> 
  {
    return new Promise(async (resolve, reject) =>
      createReadStream()
        .pipe(createWriteStream(__dirname + `/../../../images/${filename}`))
        .on('finish', () => resolve(true))
        .on('close', () => resolve(true))
        .on('error', () => reject(false))
    );
  }
}

the createReadStream make a file in your folder I think it is better to upload to your storage server directly but continue whit this, postman Is a little bit harder to use whit graphql you can try to use Altair createReadStream 在您的文件夹中创建一个文件我认为最好直接上传到您的存储服务器,但继续这样做,邮递员使用 whit graphql 有点难,您可以尝试使用Altair

Her you can see the option to Upload the file whit altair她你可以看到上传文件的选项whit altair

your Graphql query should look like this您的 Graphql 查询应如下所示

mutation ($Picture: Upload!)
{
  
  addProfilePicture
  (
    upload: $Picture,
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM