变量&$FILE"获得无效值{};上载值无效

本教程将介绍变量&$FILE"获得无效值{};上载值无效的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

变量&$FILE"获得无效值{};上载值无效 教程 第1张

问题描述

我正在使用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;获得无效值{};上载值无效的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。