Multipart payload parsing, disables POST processing for the entire GraphQL route

Viewed 182

we are using HAPI 18.4.0 extensively with GraphQL to build resilient travel experience. We want to build capability to upload files via graphQL and facing issues related to payload parsing for multipart request type. We are writing the below code onPreAuth handler.

const Hoek = require('@hapi/hoek');
const {processRequest} = require('graphql-upload');


/* Check whether the request type is 'multipart' and comes from apollo-graphql.
    Don't parse the payload and set mimetype to 'multipart/form-data'.
    Use processRequest from 'graphql-upload' to set the file data to the payload.
    Else continue.
*/


const preMultipartRequestExaminer = (path) => {
    return async (request, h) => {
        const isContentTypeAvailable = request && request.raw && request.raw.req && request.raw.req.headers
        && request.raw.req.headers['content-type'];
        if (isContentTypeAvailable
            && Hoek.reach(request, 'raw.req.headers.content-type').indexOf('multipart/form-data') > -1
            && request.path === path
            && request.method === 'post') {
            request.route.settings.payload.parse = false;
            request.mime = 'multipart/form-data';
            request.pre.payload = await processRequest(request.raw.req, request.raw.res);
        }
        return h.continue;
    };
};

I am setting request.route.settings.payload.parse = false. But there are other requests coming into picture which will throw error, as they want request.route.settings.payload.parse = true.

How can I change this and make the change per request rather than changing it at route level? And I can't restore the payload parsing to true, as the requests are async.

The solution is inspired by https://github.com/apollographql/apollo-server/issues/1680#issuecomment-446330178 (edited)

We have found the note below in the Hapi docs documenting another possible way to bypass payload processing on a per-request basis: https://hapi.dev/api/?v=19.1.1#-requestpayload

The request payload based on the route payload.output and payload.parse settings. Set to undefined in 'onRequest' extension methods and can be overridden to any non-undefined value to bypass payload processing. But this seems to be a read-only property in v18 of hapi

0 Answers
Related