Apollo-server: How to set up HTTPS with ApolloServer 2?

Created on 8 Jun 2018  Â·  10Comments  Â·  Source: apollographql/apollo-server

I'm probably missing something very obvious, but I cannot figure out how to configure HTTPS with ApolloServer…

With vanilla express, I do the following:
var https_options = {
key: key,
cert: cert
};
server = https.createServer(https_options, app).listen(PORT, HOST);

With ApolloServer, I don't seem to have the
option to configure the key and cert:

const app = express()
const server = new ApolloServer({ typeDefs, resolvers })
registerServer({ server, app })
server.listen().then(({ url }) => {
console.log(🚀 Server ready at ${url})
})

Most helpful comment

That works great! registerServer is really just an applyMiddleware call that sets up the middlewares and then you're able to call app.listen instead of server.listen. Currently it's not documented completely, so I'll leave this open for now

All 10 comments

I tried this, and it "appears" to work, but I'd like a sanity check if possible:

const app = express()
const server = new ApolloServer({ typeDefs, resolvers })
registerServer({ server, app })
// This is just a test to see if I create the HTTPS server for the app,
// will it magically work?
https.createServer(
{
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.cert')
}, app).listen(443)
console.log('Server might be ready at 443')

That works great! registerServer is really just an applyMiddleware call that sets up the middlewares and then you're able to call app.listen instead of server.listen. Currently it's not documented completely, so I'll leave this open for now

Thanks Evan... now I'm off to setting up the Subscription Server. I'll open another issue for that.

It's clear that Server 2.0 is a huge improvement. Thanks for all you do.

It's now documented and in the latest release!

In the beta.11, we moved to an applyMiddleware architecture, so the new flow looks something like: https://www.apollographql.com/docs/apollo-server/v2/essentials/server.html#middleware

Subscriptions are now created in this manner: https://glitch.com/edit/#!/mountainous-suggestion?path=index.js:49:0

When trying to follow the latest docs, I'm getting this error:

Error: To use Apollo Server with an existing express application, please use apollo-server-express

I believe this ticket should be reopened as there is no documentation on how to enable HTTPS on an ApolloServer

I thought one of the matras of AP2, is that it dramatically simplifies necessary dependencies and configuration? https should be added as a config param, imo. As setting the server up for something as fundamental as this requires additional dependencies and additional configuration.

Ahh, have just noticed, that you need to use apollo-server-express NOT apollo-server!!! So we're saying apollo-server basically doesn't support ssl? I think this is a massive ommission

@smolinari how do you set this up?

@bionicles You can follow the example here: https://www.apollographql.com/docs/apollo-server/security/terminating-ssl/ to setup an apollo server over HTTPS. However it uses apollo-server-express instead of apollo-server.

As you can see in apollo-server code, it uses an http server and has no straightforward way to replace it with an https server.

Was this page helpful?
0 / 5 - 0 ratings