0

I'm trying to implement a middleware that's able to read the content of the data sent on the request as multipart/data-form and later restore it back so express-graphql and graphql-upload can understand the request.

Since I need to read the content on the middleware, I'm using multer before to have access to the properties that are being sent. But when the request continue its course, the following errors is thrown BadRequestError: Missing multipart field ‘operations’

// first middleware
import multer from 'multer';
// ...

const middleware = (req: express.Request, res: express.Response, next: express.NextFunction) => {
  if (req.header('Content-Type')?.startsWith('multipart/form-data')) {
    const storage = multer.memoryStorage();
    return multer({ storage }).single('1')(req, res, next);
  }

  return next();
}

export default middleware;

The problem, is that after this middleware is triggered, graphql-upload is no longer able to understand the request

mutation uploadDocument($doc: DocumentInput!, $file: Upload!) {
  uploadDocument(doc: $doc, file: $file)
}

Do you know how can I restore the data on the request

fingerprints
  • 2,751
  • 1
  • 25
  • 45
  • "Multer adds a body object and a file or files object to the request object. The body object contains the values of the text fields of the form, the file or files object contains the files uploaded via the form." ... `req.originalBody = req.body`, restore at the end? – xadm Dec 08 '21 at 01:05
  • this doesn't work because it's a `multipart/data-form` data, so originally it's not on the body, that's why I need to parse it in order to read it – fingerprints Dec 08 '21 at 01:06
  • whatever, make it as earlier? `req.originalReq = req` ? – xadm Dec 08 '21 at 01:08
  • that won't work, because it's a socket, – fingerprints Dec 08 '21 at 01:10
  • stream ... https://stackoverflow.com/a/69177474/6124657 – xadm Dec 08 '21 at 01:20

0 Answers0