Set host name in GraphQLServer (graphql-yoga)


#1

Dear friends,

I have the following code:

const { GraphQLServer } = require('graphql-yoga')
const { Prisma } = require('prisma-binding')
const bodyParser = require('body-parser')
...

const db = new Prisma({
  typeDefs: process.env.PRISMA_TYPE_DEFS,
  endpoint: process.env.PRISMA_ENDPOINT,
})

const server = new GraphQLServer({
  typeDefs: process.env.QRAPHQL_TYPE_DEFS,
  resolvers,
  directiveResolvers,
  context: req => ({
    ...req,
    db
  }),
})

server.express.use( bodyParser.json({limit: "500mb"}) );
server.express.use( bodyParser.urlencoded({limit: "500mb", extended: true}) ); 
server.express.post(
  server.options.endpoint,
  checkJwt,
  (err, req, res, next) => {
    if (err) return res.status(401).send(err.message)
    next()
  }
)
server.express.post(server.options.endpoint, (req, res, next) =>
  getUser(req, res, next, db)
)

const options = {
  port: process.env.PORT,
  hostname: "xxx"
}
server.start(options, ({ port }) =>
  console.log(
    `Server started, listening on port ${port} for incoming requests.`,
  ),
)

I want to set the hostname to my domain name and use it instead of “localhost”, but unfortunately it doesn’t work.

Would you please help me?

Thanks.
David


#2

Looks like there is no current way to do this in yoga. I think you should migrate to Apollo server.

This post will help you in that regard: Should I migrate from GraphQL-Yoga to Apollo-Server?


#3

Thanks a lot for your help.