中继现代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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...