apollo-server-express vs express-graphql vs graphql-yoga
Node.js GraphQL 服务器架构选型
apollo-server-expressexpress-graphqlgraphql-yoga

Node.js GraphQL 服务器架构选型

apollo-server-express、express-graphql 和 graphql-yoga 都是用于在 Node.js 环境中构建 GraphQL API 服务器的流行库。express-graphql 是早期的官方参考实现,但目前已停止维护。apollo-server-express 是 Apollo 平台 v3 版本的核心组件,提供丰富的企业级功能,但正逐步过渡到新的 standalone 架构。graphql-yoga 是一个现代、轻量且规范兼容的服务器,基于 Envelop 构建,旨在提供最佳的开发体验和性能。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
apollo-server-express325,93613,95327.6 kB883 年前MIT
express-graphql182,5326,250-546 年前MIT
graphql-yoga08,532364 kB1406 小时前MIT

Node.js GraphQL 服务器方案深度对比:Apollo vs Express-GraphQL vs Yoga

在 Node.js 生态中构建 GraphQL API 时,选择合适的服务器库至关重要。apollo-server-express、express-graphql 和 graphql-yoga 代表了不同时期的技术选型。本文将从维护状态、配置方式、扩展能力及规范兼容性四个维度进行深度剖析,帮助架构师做出明智决策。

⚠️ 维护状态与生命周期

这是选型中最关键的因素,直接决定了项目的长期安全性。

express-graphql 已由 GraphQL 基金会正式弃用。

  • 官方不再维护,不会修复安全漏洞。
  • 社区建议立即迁移到其他方案。
// express-graphql: 已弃用,不建议使用
import { graphqlHTTP } from 'express-graphql';
// 此包不再接收更新

apollo-server-express 属于 Apollo Server v3 lineage。

  • Apollo 官方已发布 v4 版本 (@apollo/server),v3 进入维护模式。
  • 虽然仍可使用,但新功能将仅添加到 v4 架构中。
// apollo-server-express: 遗留版本
const { ApolloServer } = require('apollo-server-express');
// 官方建议迁移至 @apollo/server

graphql-yoga 处于活跃维护状态。

  • 社区活跃,定期更新以支持最新 GraphQL 特性。
  • 基于 Envelop 插件系统,架构现代且灵活。
// graphql-yoga: 现代活跃方案
import { createYoga } from 'graphql-yoga';
// 持续更新,支持最新规范

🛠️ 初始化与配置复杂度

不同的库在启动服务器所需的样板代码上有显著差异。

express-graphql 配置最简单,但功能受限。

  • 直接作为 Express 中间件使用。
  • 适合快速原型,但缺乏高级配置。
// express-graphql: 简单中间件
app.use('/graphql', graphqlHTTP({
  schema: mySchema,
  graphiql: true
}));

apollo-server-express 需要异步启动流程。

  • 必须调用 start() 方法才能应用中间件。
  • 配置项丰富,但样板代码较多。
// apollo-server-express: 异步启动
const server = new ApolloServer({ typeDefs, resolvers });
await server.start();
server.applyMiddleware({ app });

graphql-yoga 兼顾简单与灵活。

  • createYoga 函数直接返回处理请求的函数。
  • 可轻松嵌入 http 服务器或 serverless 环境。
// graphql-yoga: 灵活嵌入
const yoga = createYoga({ schema });
const server = createServer(yoga);
server.listen(4000);

🔌 扩展性与插件生态

当业务逻辑复杂时,插件系统决定了服务器的上限。

express-graphql 几乎没有扩展能力。

  • 不支持自定义插件。
  • 复杂逻辑(如权限验证)需手动在中间件层实现。
// express-graphql: 手动处理扩展
app.use('/graphql', (req, res, next) => {
  // 手动编写验证逻辑
  next();
}, graphqlHTTP({ schema }));

apollo-server-express 拥有成熟的插件系统。

  • 支持请求生命周期钩子(requestDidStart 等)。
  • 生态中有大量现成插件(缓存、监控、日志)。
// apollo-server-express: 插件支持
const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [myCustomPlugin]
});

graphql-yoga 基于 Envelop 插件系统。

  • 插件轻量且符合标准。
  • 可以复用大量 Envelop 社区插件。
// graphql-yoga: Envelop 插件
const yoga = createYoga({
  schema,
  plugins: [useLogger(), useAuth()] // 示例插件
});

📜 规范兼容性与特性

对 GraphQL 标准的支持程度影响客户端交互体验。

express-graphql 仅支持基础规范。

  • 不支持订阅(Subscriptions)。
  • 不支持批量请求(Batching)。
// express-graphql: 功能基础
// 不支持 subscriptions 配置

apollo-server-express 支持 Apollo 特有扩展。

  • 内置支持订阅和联邦(Federation)。
  • 包含一些非标准但实用的响应格式。
// apollo-server-express: 支持订阅
// 需额外配置 subscriptionServer

graphql-yoga 严格遵循官方规范。

  • 开箱支持订阅、批量请求和文件上传。
  • 响应格式标准,易于与其他工具互操作。
// graphql-yoga: 规范兼容
// 默认支持 multipart 订阅流

📊 核心特性对比总结

特性express-graphqlapollo-server-expressgraphql-yoga
维护状态❌ 已弃用⚠️ 维护模式 (v3)✅ 活跃维护
配置难度⭐ 简单⭐⭐⭐ 中等⭐⭐ 简单
插件系统❌ 无✅ 成熟 (Apollo)✅ 现代 (Envelop)
订阅支持❌ 不支持✅ 支持✅ 开箱支持
规范兼容⭐⭐ 基础⭐⭐⭐ 含扩展⭐⭐⭐⭐ 严格
适用场景学习/旧项目遗留 Apollo 系统新项目首选

💡 架构师建议

express-graphql 已成为历史。

  • 它的使命已完成,继续使用时会面临安全风险。
  • 任何还在使用它的生产项目都应制定迁移计划。

apollo-server-express 适合特定迁移场景。

  • 如果团队深度绑定 Apollo 生态(如 Studio、Federation),可暂时保留。
  • 但长期来看,应计划迁移至 @apollo/server 或评估其他方案。

graphql-yoga 是现代开发的理想选择。

  • 它在简洁性和功能之间取得了最佳平衡。
  • 对于大多数需要构建 GraphQL API 的前端或全栈团队,这是目前的最佳实践。

最终结论:除非维护旧代码,否则请忽略 express-graphql。在 apollo-server-express 和 graphql-yoga 之间,新项目应优先考虑 graphql-yoga 以获得更好的长期维护性和规范兼容性;若必须使用 Apollo 生态工具链,请直接使用最新的 @apollo/server 而非 v3 的 apollo-server-express。

如何选择: apollo-server-express vs express-graphql vs graphql-yoga

  • apollo-server-express:

    如果你的团队正在维护基于 Apollo Server v3 的遗留系统,且依赖其特定的联邦(Federation)或插件生态,可以暂时继续使用。但对于新项目,官方强烈建议迁移至 @apollo/server,因此不推荐在新架构中选用此包。

  • express-graphql:

    切勿在新项目中使用。该库已被 GraphQL 基金会正式弃用,不再接收安全更新或功能修复。仅适用于理解 GraphQL 基础原理的学习场景,生产环境应选择其他替代方案。

  • graphql-yoga:

    如果你需要一个新的、轻量级且符合 GraphQL 规范的服务器,这是首选方案。它开箱即用,支持流式响应,且易于集成到各种 serverless 或边缘计算环境中,适合追求现代技术栈的团队。

apollo-server-express的README

npm version Build Status Join the community forum Read CHANGELOG

This is the Express integration of Apollo Server. Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks. Read the docs. Read the CHANGELOG.

A full example of how to use apollo-server-express can be found in the docs.

Before Apollo Server 3, we officially supported using this package with connect as well. connect is an older framework that express evolved from. For now, we believe that this package is still compatible with connect and we even run tests against connect, but we may choose to break this compatibility at some point without a major version bump. If you rely on the ability to use Apollo Server with connect, you may wish to make your own integration.

Principles

GraphQL Server is built with the following principles in mind:

  • By the community, for the community: GraphQL Server's development is driven by the needs of developers
  • Simplicity: by keeping things simple, GraphQL Server is easier to use, easier to contribute to, and more secure
  • Performance: GraphQL Server is well-tested and production-ready - no modifications needed

Anyone is welcome to contribute to GraphQL Server, just read CONTRIBUTING.md, take a look at the roadmap and make your first PR!