graphqlUploadExpress.mjs 751 B

1234567891011121314151617181920212223242526
  1. import { processRequest as defaultProcessRequest } from './processRequest'
  2. export const graphqlUploadExpress = ({
  3. processRequest = defaultProcessRequest,
  4. ...processRequestOptions
  5. } = {}) => (request, response, next) => {
  6. if (!request.is('multipart/form-data')) return next()
  7. const finished = new Promise(resolve => request.on('end', resolve))
  8. const { send } = response
  9. response.send = (...args) => {
  10. finished.then(() => {
  11. response.send = send
  12. response.send(...args)
  13. })
  14. }
  15. processRequest(request, response, processRequestOptions)
  16. .then(body => {
  17. request.body = body
  18. next()
  19. })
  20. .catch(error => {
  21. if (error.status && error.expose) response.status(error.status)
  22. next(error)
  23. })
  24. }