如何解决中继现代BadRequestError:缺少多部分字段“ operations”
我正在尝试将文件上传到我的服务器。使用Altair,我可以做到这一点而没有任何错误,但是当我使用Relay.js上载它时,服务器会引发以下错误。
BadRequestError: Missing multipart field ‘operations’ (https://github.com/jaydenseric/graphql-multipart-request-spec).
at Busboy.<anonymous> (/home/dotsinspace/Documents/dev/truck.pe__server/node_modules/.pnpm/graphql-upload@9.0.0_graphql@15.3.0/node_modules/graphql-upload/processRequest.js:362:11)
at Object.onceWrapper (events.js:420:28)
at Busboy.emit (events.js:326:22)
at Busboy.EventEmitter.emit (domain.js:486:12)
at Busboy.emit (/home/dotsinspace/Documents/dev/truck.pe__server/node_modules/.pnpm/busboy@0.3.1/node_modules/busboy/lib/main.js:37:33)
at /home/dotsinspace/Documents/dev/truck.pe__server/node_modules/.pnpm/busboy@0.3.1/node_modules/busboy/lib/types/multipart.js:52:13
at processTicksAndRejections (internal/process/task_queues.js:75:11)
以下是我尝试提交的我的graphql代码和变异。
#Graphql
graphql`
mutation AccountUploadMutation($profilePicture: Image!) {
AccountUpload(profilePicture: $profilePicture) {
message,status
}
}
`
#Mutation
commitMutation(Environment,{
'mutation': AccountUploadMutation,'variables': { 'profilePicture': v },'uploadables': { 'file': v },'onCompleted': (response,error) => Response({},{ response,error })
})
对于将零件上传到.upables中必须提供文件,我感到非常困惑。但是我的服务器使用profilePicture作为图像查找变量,我该如何处理。
解决方法
您在后端的多部分解析配置中似乎遇到了问题。
我的猜测是中继Network
在mutlipart字段“ operation”中发送您的GraphQL查询,但是您的后端正在寻找字段“ operations”(复数)。要解决该错误,请在Network
字段中确认您的operations
正在发送查询,或者更改您的后端以读取其实际发送的任何字段。
另一种可能性是您根本不发送多部分格式的查询。如果您按照Network
文档的示例发送请求,那么您只是在发送JSON对象,而不是多部分形式:
// Example from Relay docs. Sends a JSON object,not a multipart
// form as expected by your backend
function fetchQuery(
operation,variables,cacheConfig,uploadables,) {
return fetch('/graphql',{
method: 'POST',headers: {
// Add authentication and other headers here
'content-type': 'application/json'
},body: JSON.stringify({
query: operation.text,// GraphQL text from input
variables,}),}).then(response => {
return response.json();
});
}
// Create a network layer from the fetch function
const network = Network.create(fetchQuery);
在这种情况下,编写fetchQuery
函数以使用多部分形式获取数据。请参见以下示例:fetch post with multipart form data
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。