【发布时间】:2020-06-24 15:48:51
【问题描述】:
我在 KeystoneJS AdminUI 的自定义字段中使用 TinyMCE,这是一个 React 应用程序。我想将 React 前端的图像上传到 KeystoneJS GraphQL 后端。我可以使用添加到 Keystone 服务器的 REST 端点上传图像——向 TinyMCE 传递一个images_upload_handler 回调——但我想利用 Keystone 已经构建的 GraphQL 端点来获取图像列表/类型创建的。
我首先尝试使用this article中详述的方法,使用axios上传图片
const getGQL = (theFile) => {
const query = gql`
mutation upload($file: Upload!) {
createImage(file: $file) {
id
file {
path
filename
}
}
}
`;
// The operation contains the mutation itself as "query"
// and the variables that are associated with the arguments
// The file variable is null because we can only pass text
// in operation variables
const operation = {
query,
variables: {
file: null
}
};
// This map is used to associate the file saved in the body
// of the request under "0" with the operation variable "variables.file"
const map = {
'0': ['variables.file']
};
// This is the body of the request
// the FormData constructor builds a multipart/form-data request body
// Here we add the operation, map, and file to upload
const body = new FormData();
body.append('operations', JSON.stringify(operation));
body.append('map', JSON.stringify(map));
body.append('0', theFile);
// Create the options of our POST request
const opts = {
method: 'post',
url: 'http://localhost:4545/admin/api',
body
};
// @ts-ignore
return axios(opts);
};
但我不确定要传递什么 theFile -- TinyMCE 的 images_upload_handler,我需要从中调用图像上传,接受一个 blobInfo 对象,其中包含给我的功能
文件名不起作用,blob 也不起作用——两者都给我服务器错误 500——错误消息没有更具体。
我更喜欢使用 GraphQL 客户端来上传图片——another SO article 建议使用apollo-upload-client。但是,我在 KeystoneJS 环境中操作,Apollo-upload-client 说
Apollo 客户端只能有 1 个“终止”Apollo Link 发送 GraphQL 请求;如果已经设置了诸如 apollo-link-http 之类的, 删除它。
我相信 Keystone 已经设置了Apollo-link-http(它在搜索中多次出现),所以我认为我不能使用Apollo-upload-client。
【问题讨论】:
标签: node.js graphql apollo-client apollo-server keystonejs