11

I am struggling to understand the added value of Express (or Koa, Hapi, etc) integration with Apollo GraphQL server.

I see it can work in stand alone mode very well (an example: https://medium.com/codingthesmartway-com-blog/apollo-server-2-introduction-efc4026f5654).

In which case should we use it with (or without) integration? What should drive this decision?

Aleks
  • 5,674
  • 1
  • 28
  • 54

1 Answers1

15

If all you need is a GraphQL endpoint, then using the standalone library (apollo-server) is generally preferred because there will be less boilerplate to write (features like subscriptions, file uploads, etc. just work without additional configuration). However, many applications require additional functionality beyond just exposing a single API endpoint. Examples include:

  • Webhooks
  • OAuth callbacks
  • Session management
  • Cookie parsing
  • CSRF protection
  • Monitoring or logging requests
  • Rate limiting
  • Geofencing
  • Serving static content
  • Server-side rendering

If you need this sort of functionality for your application, then you'll want to utilize an HTTP framework like Express and then use the appropriate integration library (i.e. apollo-server-express).

Apollo Server also includes integrations for serverless solutions AWS Lambda. If you want to go serverless to, for example, get better scalability or eliminate system admin costs, then you would also need to use one of these integrations.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • Thank you Daniel, very clear answer, I get it. Just one small thing - your last statement. Why do you say the integration is necessary to deploy to AWS? It should be easy to build just a stand alone Apollo server, no integrations, and deploy it to Lambda or Fargate. Did I understand you well? – Aleks May 08 '20 at 09:17
  • I haven't used Lambda extensively, but my understanding was that you no longer use [listen](https://nodejs.org/api/net.html#net_server_listen) when running an HTTP server since there is effectively no port to listen to and everything runs through the handler function itself. Under the hood, the standalone library just sets up an Express server for you and then calls `listen` when you call `listen` on the server instance. – Daniel Rearden May 08 '20 at 10:50
  • The greater point is that when using serverless solutions like Azure Functions, Google Cloud Functions, etc. the architecture is different enough compared to a regular Express app to warrant using the appropriate integration. – Daniel Rearden May 08 '20 at 10:55