【发布时间】:2021-10-12 09:37:12
【问题描述】:
我正在构建下一个 js 应用程序。我正在尝试将文件上传到 graphql apollo 服务器。这是突变-
mutation signUp($avatar: Upload!) {
signUp(
avatar: $avatar
input: {
name: "Siam Ahnaf"
email: "siamahnaf198@yahoo.com"
password: "siam1980"
}
){
message
token
}
}
在下一个 js 应用程序中,我使用 axios 发布请求。最需要的功能-
import FormData from "form-data";
import fs from "fs";
export const signUp = (data) => async (dispatch) => {
dispatch({ type: SIGNUP_LOADING })
var info = new FormData();
info.append('operations', `{ "query": "mutation($file: Upload!) {signUp(avatar: $file, input: {name: "Siam Ahnaf", email: "siamahnaf198@yahoo.com", password: "siam1980"}){message, token}}", "variables": { "file": null } }`);
info.append('map', '{ "0": ["variables.file"] }');
info.append('0', fs.createReadStream(data.picture[0]));
axios.post("http://localhost:3001/graphql", info, {
headers: {
...info.getHeaders()
}
})
.then(res => console.log(res))
.catch(err => console.log(err));
}
为了使用 fs,我在 next.config.js 中编写了一些代码-
module.exports = optimizedImages({
images: {
disableStaticImages: true,
},
webpack5: true,
webpack: (config) => {
config.resolve.fallback = { fs: false };
return config;
},
});
【问题讨论】:
-
那是javascript/webpack的问题。
-
这是否回答了您的问题:fs.createReadStream is not a function?您不能在浏览器中使用
fs模块,它只能在 Node.js 环境中使用。 -
那么,如何将formdata发送到graphql服务器?
标签: javascript webpack