question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hapi 17 multipart not working

See original GitHub issue

I use Apollo on top of Hapi 17 and I can’t find a way to make file upload work in my project so I made a simple example based on documentation and find out that it’s not working either. Here’s the repository for reproduction of the bug: https://github.com/D34THWINGS/test-hapi-apollo-upload

From what i’ve found so far, Hapi has not yet processed the request when onRequest extension endpoint is called. So the request.mime is null and the processFileUploads is therefore not called (ref ApolloServer.ts#L20)

I then tried some modifications:

  • Moving to onPreHandler extension point. This make the mime.type set to multipart/form-data and making us going further but still not working.
  • Passing request.raw.req to processFileUploads. This allows the code to make it further but this time the request hangs forever and I can’t seem to find why.

EDIT: Also tried the following:

  • Use request.headers.slice(0, 19) instead of request.mime
  • Passing request.raw.req to processFileUploads. But now throws: Cannot assign to read only property 'payload' because Hapi is later trying to modify payload which has been marked as read only property within handleFileUploads.

EDIT 2: After making some extensive changes to the lib I found out that the parsing of the payload should be disabled for ‘POST’ on the /graphql route so the apollo-upload-server can do it’s own parsing. But I think it should be better to have an implementation for Hapi that we should call within the request handler.

So if anyone with a better understanding can help me going through this, I would be happy to make a PR.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
wesselvdvcommented, Dec 11, 2018

I am running into this issue big time as well, I did however get it to work by dynamically turning on and off payload parsing in combination with manual parsing for file uploads.


const { ApolloServer, gql } = require('apollo-server-hapi');
const Hapi = require('hapi');
const { processRequest } = require('apollo-upload-server');

async function StartServer() {
  const server = new ApolloServer({ typeDefs, resolvers });

  const app = new Hapi.server({
    port: 4000
  });

  await server.applyMiddleware({
    app,
    route: {
      cors: process.env.NODE_ENV === 'development',
      auth: {
        mode: 'try'
      },
      ext: {
        onPreAuth: {
          method: async (request, h) => {
            if (
              request.raw.req.headers['content-type'].indexOf(
                'multipart/form-data'
              ) > -1 &&
              request.path === server.graphqlPath &&
              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
              );
            } else {
              request.route.settings.payload.parse = true;
            }

            return h.continue;
          }
        },
        onPostAuth: {
          method: async (request, h) => {
            if (request.pre['payload']) {
              request.payload = request.pre['payload'];
            }

            return h.continue;
          }
        }
      }
    }
  });

  await server.installSubscriptionHandlers(app.listener);

  await app.start();
}

StartServer().catch(error => console.log(error));
1reaction
martijnwalravencommented, Jul 8, 2019

Glad to hear that workaround does the job. Closing this issue because we won’t be directly maintaining integrations with specific frameworks in Apollo Server 3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Hapi 17 multipart not working - - Bountysource
I use Apollo on top of Hapi 17 and I can't find a way to make file upload work in my project so...
Read more >
hapi — How to Fix “Unsupported Media Type” - Future Studio
The file is an Excel sheet and at first, it seemed like the content type was the issue. But the problem was the...
Read more >
HapiJS not accepting form-data requests - Stack Overflow
... of HapiJS I'm using I found someone else had the same problem. ... error 415 unsupported media type upload file with multipart/form-data....
Read more >
21.1.0 API Reference - hapi.dev
API v21.1.x. Server. The server object is the main application container. The server manages all incoming requests along with all the facilities provided...
Read more >
hapijs/hapi - Gitter
hello hapijs gurus, I've been puzzled by this problem ... the graph is well inserted so I return it (using hapi 17 and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found