jerome
jerome

Reputation: 4987

Support additional mime types in hapi

I am working on a graphql + relay app served on top of hapi and would like to support requests to the graphql endpoint with the application/graphql mime type.

Here you can see me sending POST requests to the graphql endpoint.

~> curl -X POST --header "Content-Type:application/json" -d '{"query":"{content(id:\"13381672\"){title,id}}"}' http://127.0.0.1:8000/graphql
{"data":{"content":{"title":"Example Title","id":"13381672"}}}

~> curl -X POST --header "Content-Type:application/graphql" -d '{"query":"{content(id:\"13381672\"){title,id}}"}' http://127.0.0.1:8000/graphql
{"statusCode":415,"error":"Unsupported Media Type"}

I do not see any place in my hapi server options where there is any explicit configuration for mime types and other than some terse documentation here.

I have set up an options mime configuration as per the below, passing options into the server instantiation, but I am still seeing the "Unsupported Media Type" error.

options.mime = {
  override: {
    'application/graphql': {
      charset: 'UTF-8',
      compressible: true,
      type: 'application/graphql'
    }
  }
};

Does anybody else here have this kind of experience with hapi?

Upvotes: 0

Views: 1864

Answers (1)

mikefrey
mikefrey

Reputation: 4691

Each route has a payload config option which takes an allow property that lets hapi know which mimetypes to allow for that route. If you set it to application/graphql, and the parse option to false, your requests will work.

Unfortunately, you'll have to parse the payload yourself.

Here's an example route:

server.route({
  method: ['POST', 'PUT'],
  path: '/graphql',
  config: {
    payload: {
      parse: false,
      allow: 'application/graphql'
    }
  },
  handler: function(request, reply) {
    reply(request.payload)
  }
})

Upvotes: 2

Related Questions