小编Geo*_*ios的帖子

GraphQL 文件上传的拒绝和错误处理

我有一个带有接受单个文件的解析器的快速 GraphQL 端点。解析器包括对正在接收的文件的简单验证。

问题在于,当验证失败时,没有办法立即将错误返回给前端,因为抛出错误并不会强制中断上传请求。

一个过于简单的例子:

fileUpload: async (parent, { file, otherdata }) => {
    const isValid = (some logic here)
    if(!isValid)
        throw new ApolloError('upload parameters not valid')

    //No need to get this far,
    //we could have rejected the request already by examining otherdata,
    //or the stream could be already created for max time utilization

    const { createReadStream, filename, mimetype, encoding } = await file;
    const readStream = await createReadStream()
    ...
 }
Run Code Online (Sandbox Code Playgroud)

预期行为:解析器返回通常的 {errors:[], data:null} 对象 - 或错误本身 - 取决于错误策略选项。 …

error-handling upload file-upload graphql express-graphql

5
推荐指数
1
解决办法
855
查看次数