apollo-server-express、express-graphql 和 graphql-yoga 都是用于在 Node.js 环境中构建 GraphQL API 服务器的流行库。express-graphql 是早期的官方参考实现,但目前已停止维护。apollo-server-express 是 Apollo 平台 v3 版本的核心组件,提供丰富的企业级功能,但正逐步过渡到新的 standalone 架构。graphql-yoga 是一个现代、轻量且规范兼容的服务器,基于 Envelop 构建,旨在提供最佳的开发体验和性能。
在 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/server),v3 进入维护模式。// apollo-server-express: 遗留版本
const { ApolloServer } = require('apollo-server-express');
// 官方建议迁移至 @apollo/server
graphql-yoga 处于活跃维护状态。
// graphql-yoga: 现代活跃方案
import { createYoga } from 'graphql-yoga';
// 持续更新,支持最新规范
不同的库在启动服务器所需的样板代码上有显著差异。
express-graphql 配置最简单,但功能受限。
// 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 函数直接返回处理请求的函数。// 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 拥有成熟的插件系统。
// apollo-server-express: 插件支持
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [myCustomPlugin]
});
graphql-yoga 基于 Envelop 插件系统。
// graphql-yoga: Envelop 插件
const yoga = createYoga({
schema,
plugins: [useLogger(), useAuth()] // 示例插件
});
对 GraphQL 标准的支持程度影响客户端交互体验。
express-graphql 仅支持基础规范。
// express-graphql: 功能基础
// 不支持 subscriptions 配置
apollo-server-express 支持 Apollo 特有扩展。
// apollo-server-express: 支持订阅
// 需额外配置 subscriptionServer
graphql-yoga 严格遵循官方规范。
// graphql-yoga: 规范兼容
// 默认支持 multipart 订阅流
| 特性 | express-graphql | apollo-server-express | graphql-yoga |
|---|---|---|---|
| 维护状态 | ❌ 已弃用 | ⚠️ 维护模式 (v3) | ✅ 活跃维护 |
| 配置难度 | ⭐ 简单 | ⭐⭐⭐ 中等 | ⭐⭐ 简单 |
| 插件系统 | ❌ 无 | ✅ 成熟 (Apollo) | ✅ 现代 (Envelop) |
| 订阅支持 | ❌ 不支持 | ✅ 支持 | ✅ 开箱支持 |
| 规范兼容 | ⭐⭐ 基础 | ⭐⭐⭐ 含扩展 | ⭐⭐⭐⭐ 严格 |
| 适用场景 | 学习/旧项目 | 遗留 Apollo 系统 | 新项目首选 |
express-graphql 已成为历史。
apollo-server-express 适合特定迁移场景。
@apollo/server 或评估其他方案。graphql-yoga 是现代开发的理想选择。
最终结论:除非维护旧代码,否则请忽略 express-graphql。在 apollo-server-express 和 graphql-yoga 之间,新项目应优先考虑 graphql-yoga 以获得更好的长期维护性和规范兼容性;若必须使用 Apollo 生态工具链,请直接使用最新的 @apollo/server 而非 v3 的 apollo-server-express。
如果你的团队正在维护基于 Apollo Server v3 的遗留系统,且依赖其特定的联邦(Federation)或插件生态,可以暂时继续使用。但对于新项目,官方强烈建议迁移至 @apollo/server,因此不推荐在新架构中选用此包。
切勿在新项目中使用。该库已被 GraphQL 基金会正式弃用,不再接收安全更新或功能修复。仅适用于理解 GraphQL 基础原理的学习场景,生产环境应选择其他替代方案。
如果你需要一个新的、轻量级且符合 GraphQL 规范的服务器,这是首选方案。它开箱即用,支持流式响应,且易于集成到各种 serverless 或边缘计算环境中,适合追求现代技术栈的团队。
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.
GraphQL Server is built with the following principles in mind:
Anyone is welcome to contribute to GraphQL Server, just read CONTRIBUTING.md, take a look at the roadmap and make your first PR!