0
votes

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?

1
Not sure if this is helpful but in this example (github.com/SimonDegraeve/hapi-graphql/blob/master/src/index.js) the author created a new handler which checks for mime type.Kevin Wu
Sure...I have actually ported (modified) this code for my hapi server, and it works fine for the most part. We see here (github.com/SimonDegraeve/hapi-graphql/blob/master/src/…) that the author is checking for the mime-type (as do I). But the issue I have is I do not see where/how to register this additional non-standard mime type. My cURL (above) with the application/graphql type does not even make it through to the parsePayload() function. It is being thrown out with a 415 error long before that.jerome
Are you still having this issue? That should work.arb
I'm not having any direct issues, as I learned that my relay front end is making requests of a standard mime-type. At some point I would still like how to teach hapi to accept non-standard types, but this is a non-issue for me, now.jerome

1 Answers

2
votes

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)
  }
})