gpt4 book ai didi

javascript - 变量 "$file"的值无效 {};上传值无效

转载 作者:行者123 更新时间:2023-12-04 12:14:01 25 4
gpt4 key购买 nike

我正在使用 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":"\n mutation uploadProfileImage($file: Upload!){\n uploadProfileImage(file: $file)\n }\n",
"variables":{"file":null}
}
map: {"1":["variables.file"]}
1: (binary)
其他人遇到过这个问题吗?我似乎无法将文件上传到我的后端。

最佳答案

我通过在 ApolloServer 中将上传选项设置为 false 来解决此问题。配置。

new ApolloServer({ schema, context, uploads: false })
然后使用 graphqlUploadExpress()来自 graphql-upload 的中间件.
app.use(graphqlUploadExpress({ maxFileSize: 10000, maxFiles: 10 }));
希望这可以帮助遇到我遇到同样问题的任何人😊

关于javascript - 变量 "$file"的值无效 {};上传值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64658321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com