为什么要用Apollo GraphQL Sever的Express(或其他集成)?[英] Why Express (or other integration) with Apollo GraphQL Sever?

本文是小编为大家收集整理的关于为什么要用Apollo GraphQL Sever的Express(或其他集成)?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在努力理解与Apollo GraphQl Server集成的Express(或KOA,HAPI等)的附加值.

我看到它可以在独立模式下很好地工作(一个示例: https://medium.com/codingthesmartway-com-blog/apollo-server-2-indroduction-efc4026f5654 ).

在哪种情况下,我们应该(或不)集成?该决定应该做什么?

推荐答案

如果您只需要一个GraphQl端点,则通常首选使用独立库(apollo-server),因为要写的样板较少(诸如订阅,文件上传等功能,只需使用其他其他功能配置).但是,许多应用程序还需要其他功能,而不仅仅是公开单个API端点.示例包括:

  • webhooks
  • oauth回调
  • 会话管理
  • cookie解析
  • CSRF保护
  • 监视或记录请求
  • 利率限制
  • Geopencing
  • 服务静态内容
  • 服务器端渲染

如果您需要用于应用程序的此类功能,那么您需要使用诸如Express之类的HTTP框架,然后使用适当的集成库(即apollo-server-express).

Apollo服务器还包括无服务器解决方案AWS Lambda的集成.如果您想无服务器,例如获得更好的可伸缩性或消除系统管理成本,那么您也需要使用其中一种集成.

本文地址:https://www.itbaoku.cn/post/1938031.html

问题描述

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?

推荐答案

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.