变量&$FILE"获得无效值{};上载值无效 [英] Variable "$file" got invalid value {}; Upload value invalid

查看:29
本文介绍了变量&$FILE"获得无效值{};上载值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用graphql-request中的GraphQLClient向我的服务器发送请求。我正在尝试通过执行以下操作上载文件:

const graphQLClient = new GraphQLClient('http://localhost:4000/graphql', {
    credentials: 'include',
    mode: 'cors',
});
const source = gql`
    mutation uploadImage($file: Upload!) {
        uploadImage(file: $file)
    }
`;
const file: RcFile = SOME_FILE; // RcFile (from antd) extends File
await graphQLClient.request<{uploadImage: boolean}>(source, { file });

但是,当我以这种方式向我的服务器发送请求时,我收到以下错误:

GraphQLError: Variable "$file" got invalid value {}; Upload value invalid

我的请求在控制台中如下所示:

operations: {
    "query":"
 mutation uploadProfileImage($file: Upload!){
 uploadProfileImage(file: $file)
 }
",  
    "variables":{"file":null}
}
map: {"1":["variables.file"]}
1: (binary)

其他人有过这个问题吗?我似乎无法将文件上载到我的后端。

推荐答案

我已通过将ApolloServer配置中的上载选项设置为FALSE修复了该问题。

new ApolloServer({ schema, context, uploads: false })

,然后使用graphql-upload中的graphqlUploadExpress()中间件。

app.use(graphqlUploadExpress({ maxFileSize: 10000, maxFiles: 10 }));

希望这能帮助任何遇到和我遇到😊相同问题的人

这篇关于变量&$FILE&QUOT;获得无效值{};上载值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆