NodeJS Apollo-express-server仅传递第一次上传(文件),其余部分缺失

ffscu2ro  于 2023-02-08  发布在  Node.js
关注(0)|答案(1)|浏览(92)

我正在使用apollo-express服务器和GraphQL。我有一个突变,我从前端传递文件到后端,但我只收到第一个的file:{}对象-对于其他的,我收到承诺。
以下是我的突变宣言:

extend type Mutation {
    createCase(input: CreateCase!, attachments: [Upload]): Case
  }

我简化了我的实现,只使用console.log记录附件:

Mutation: {
    createCase: async (
      parentValue: any,
      { input, attachments }: { input: CaseInterface; attachments: [File] },
      context: any
    ) => {
     
      console.log(attachments)
    }
}

我从前端传递文件,如下所示:

const SEND_CASE = gql`
  mutation CreateCase($input: CreateCase!, $attachments: [Upload]) {
    createCase(input: $input, attachments: $attachments) {
      _id
    }
  }
`;

函数用法:

createCase({
      variables: {
        input: {
          description: props.creationData.description,
          date: new Date().toDateString(),
          priority: props.creationData.priority,
          userId: props.creationData.userId,
          categoryId: props.categories.map((el: any) => el._id),
          signature: "",
          type: props.casetype === "problem" ? 1 : 2,
        },
        attachments: props.creationData.attachments,
      },
    });

prop.creationData.attachments看起来正常:

问题是,在我的后端I console.log(附件)看起来像这样:

[
  Upload {
    resolve: [Function (anonymous)],
    reject: [Function (anonymous)],
    promise: Promise { [Object] },
    file: {
      filename: 'wa.jpg',
      mimetype: 'image/jpeg',
      encoding: '7bit',
      createReadStream: [Function: createReadStream]
    }
  },
  Upload {
    resolve: [Function (anonymous)],
    reject: [Function (anonymous)],
    promise: Promise { <pending> }
  }
]

第二个Upload对象中的文件丢失,并且那里的Promise处于挂起状态。我无法解释为什么会这样。如果需要,我还可以使用console.log记录req.body和req.header:

{
  host: '192.168.1.152:3001',
  connection: 'keep-alive',
  'content-length': '51479',
  accept: '*/*',
  'apollo-require-preflight': 'true',
  authorization: '',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
  'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryA2VfmoLeZm9NTXsk',
  origin: 'http://localhost:3000',
  referer: 'http://localhost:3000/',
  'accept-encoding': 'gzip, deflate',
  'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,bg;q=0.7'
}
{
  operationName: 'CreateCase',
  variables: {
    input: {
      description: 'test',
      date: 'Thu Jan 19 2023',
      priority: 1,
      userId: '630899dc48a9a14833398a7e',
      categoryId: [Array],
      signature: '',
      type: 1
    },
    attachments: [ [Upload], [Upload] ]
  },
  query: 'mutation CreateCase($input: CreateCase!, $attachments: [Upload]) {\n' +
    '  createCase(input: $input, attachments: $attachments) {\n' +
    '    _id\n' +
    '    __typename\n' +
    '  }\n' +
    '}'
}

我怎么能有我的所有文件在我的后端是一样的第一个收到?

1tu0hz3e

1tu0hz3e1#

两个文件都“在那里”,但是到目前为止只有一个完成了上传。不过它们都是承诺,所以处理它们是一样的。你只需要等待承诺解决,你应该没事。
根据graphql-upload examples repo,假设您有一个类似storeUpload的方法,一次处理一个文件,您可以执行以下操作:

const resolvers = {
  Mutation: {
    createCase: async (
      parentValue: any,
      { input, attachments }: { input: CaseInterface; attachments: [File] },
      context: any
    ) => {
      const attachmentFilenames = [];

      // Ensure an error storing one upload doesn’t prevent storing the rest.
      for (const result of await Promise.allSettled(attachments.map(storeUpload))) {
        if ("value" in result) {
          attachmentFilenames.push(result.value);
        } else {
          // Realistically you would do more than just log an error.
          console.error(`Failed to store upload: ${result.reason}`);
        }
      }
      console.log(attachmentFilenames)
    }
  },
};

相关问题