MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

一个 .NET 团队把 GraphQL 做成了全家桶:ChilliCream/graphql-platform 深度拆解

项目背景与痛点

.NET 生态做 GraphQL,长期只有 GraphQL.NET 这一个像样的选项:能跑,但工程化能力薄弱——没有 Federation、没有配套的客户端代码生成、性能也只能算“够用”。与此同时,前端侧的 Apollo 早已把「服务端 + 客户端 + IDE」串成一条链。

ChilliCream/graphql-platform 就是来补这条链的。它不是一个库,而是三个产品:

  • Hot Chocolate:服务端,schema 优先或代码优先,编译到 ASP.NET Core 请求管线;
  • Strawberry Shake:客户端,把 .graphql 文件在编译期变成强类型 C# 代码;
  • Nitro:基于 Monaco 的 GraphQL IDE,带 schema 感知补全。

真正解决的核心问题是契约漂移。当服务端 schema、客户端类型、调试工具三者各自为政时,任何字段改名都要靠人工对齐。这套组合让 schema 成为唯一事实来源,从服务端一路贯穿到客户端编译期。这一点比“性能更好”重要得多。

核心技术揭秘

Hot Chocolate 与多数 GraphQL 服务端最本质的差异,在于它不解释执行 AST,而是把请求文档编译成可缓存的执行计划。

一次请求进来,管线大致是:解析 → 规范化校验 → Operation Compiler 把选择集内联、片段展开、字段合并、变量提取,产出一棵与具体变量值解耦的“操作树” → 执行引擎按变量值驱动这棵树。缓存键是文档哈希(文档 ID),命中后直接跳过解析与校验。这是它性能数据的来源。

第二个关键点是解析器委托化。字段 resolver 在 schema 构建阶段就被烘焙成委托,而不是运行时反射调用;结合服务注册作用域,避免了每字段一次的反射开销。

第三个是 IQueryable 投影。当 resolver 返回 IQueryable<T> 时,执行引擎把 GraphQL 选择集反向翻译成 LINQ 表达式树,交给 EF Core 生成 SQL——只 SELECT 客户端要的列,并把嵌套关联压成 JOIN 或分批查询。配合 DataLoader 的批处理与请求内缓存,N+1 基本被消灭。

graph TD A[客户端 GraphQL 请求] --> B[ASP.NET Core 中间件] B --> C{"文档缓存命中?"} C -->|"命中"| E[取出已编译执行计划] C -->|"未命中"| D[解析与规范校验] D --> F[Operation Compiler 编译] F --> G[写入文档缓存] G --> E E --> H[执行引擎调度字段解析器] H --> I[DataLoader 批处理去重] I --> J[数据源 EF Core 或下游微服务] J --> K[结果树序列化] K --> L[GraphQL 响应]

客户端侧走的是另一条路:Strawberry Shake 用 Roslyn 源生成器,在编译期读取 .graphql 与 schema,为每个查询生成 DTO、序列化器和类型安全的调用方法。查询改了、schema 不匹配,编译直接失败——错误被左移到构建阶段。

功能亮点与差异

  • 双 schema 模式:既支持 SDL 文件(schema-first),也支持 C# 类 + 特性注解(code-first),后者还能继续用源生成器消除样板代码。很多团队是混合使用。
  • Federation 支持完整度:Apollo Federation v2 的实体、@key、@shareable、子图合并都覆盖到了,这是它在 .NET 阵营的核心差异化优势。
  • 增量交付:@defer / @stream 的实现,让大响应能分块下发。
  • 端到端工具链:Nitro 直接吃服务端 schema 做补全,比外挂 GraphiQL 少一层心智负担。
  • GraphQL over HTTP 规范与 APQ:自动持久化查询开箱可用,这对生产环境减少请求体体积和防止任意查询注入很实用。

应用场景与落地建议

适合:微服务聚合层 / BFF、需要多端(Web、App、第三方)差异化取数的中大型系统、已有 EF Core 数据层且希望按需投影的场景。

需要谨慎:

  1. IQueryable 投影不是万能翻译器。复杂业务逻辑一旦被塞进 resolver 返回的查询表达式,翻译出的 SQL 可能远超预期。建议对投影查询做 SQL 审查,复杂聚合老老实实用手写 resolver。
  2. DataLoader 必须是 Scoped。注册成 Singleton 是经典事故——缓存跨请求泄漏,直接串数据。
  3. 必须上复杂度限制与深度限制。MaxExecutionDepth、复杂度分析器、持久化查询白名单,三者至少上两个,否则一个嵌套查询就能打穿数据库。
  4. 订阅要横向扩展,需要引入 Redis backplane,否则多实例下事件只落在单个节点。
  5. 版本升级不要轻视。v13 到 v14 在管线 API、命名上有破坏性改动,预留迁移窗口。

综合评价

优点:全链路一致性强,schema 到客户端零漂移;执行计划缓存 + 委托化 + 投影的组合,让它在 .NET GraphQL 服务端里性能处于第一梯队;Federation 支持成熟,能直接进微服务架构。

缺点:三件套耦合度高,用 Hot Chocolate 往往会被“顺带”拉进 Strawberry Shake 与 Nitro 的世界;官方文档量大但碎片化,很多行为要翻源码或社区讨论才能确认;抽象层数深,出问题时堆栈对新手不友好;v14 的增量交付等新特性仍在演进,生产采用前需评估稳定性。

一句话:如果 .NET 团队认真要做 GraphQL,这是目前唯一值得作为架构基座评估的方案;但如果只是想给现有 REST 加个查询层,它的学习曲线和抽象成本可能反而成为负担。

关于

GitHub 原始描述(英文)

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Nitro the awesome Monaco based GraphQL IDE.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

C#
CSS
Dockerfile
HTML
JavaScript
Open Policy Agent
PowerShell
Python
Rust
Shell
Tape
TypeScript

Release

README

NuGet Package License Slack channel Twitter


ChilliCream GraphQL Platform

Welcome to the ChilliCream GraphQL Platform!

We help you, developers and companies, to leverage your APIs to the next level with GraphQL. Strongly-typed schemas that match your APIs 100 percent. Efficient data fetching that reduces overall cost without extra effort. Consumer-friendly, declarative, self documented APIs that support you in your daily work to build powerful UIs effectively.

Most of our products are open-source and right here in this repository.

Everyone is welcome! Always remember to treat anyone respectful, no matter their gender, opinion, religion, or skin-tone. We’re one world and together we’re stronger!

Join our awesome community on Slack, if you would like to get in touch with us, need help, or just want to learn!

Our Products

Hot Chocolate

Hot Chocolate is the most efficient, feature-rich, open-source GraphQL server in the .NET ecosystem, that helps developers to build powerful GraphQL APIs and Gateways with ease.

Documentation

Nitro

Nitro App is an awesome, performant, feature-rich GraphQL IDE / API Cockpit that helps developers and data scientists to explore, share, and test any GraphQL API.

Nitro App can be installed as a Desktop App, used as a Web App, which can be installed through your browser of choice as well, or used as middleware on your GraphQL endpoint. Middlewares are available in .NET and NodeJS. More middlewares will follow.

Documentation

Strawberry Shake

Strawberry Shake is an incredible, open-source GraphQL client for the .NET ecosystem, that helps developers to build awesome UIs in Blazor, Maui, and more. Other than most .NET GraphQL clients, Strawberry Shake is type-safe GraphQL client that generates .NET types from your GraphQL schema out of the box. Moreover, Strawberry Shake comes with a reactive store like in Relay and Apollo Client, which is nice due to the fact that you can build reactive UIs in .NET with stuff like client-side data caching and data-fetching strategies.

Documentation

Green Donut

Green Donut is a lightweight, yet powerful DataLoader that simplifies batching, caching, and solves the N+1 problem.

Documentation

Official examples

Examples of things built on top of the ChilliCream GraphQL Platform that are open source and can be explored by others.

Contributing

Become a code contributor and help us make the ChilliCream GraphQL platform even better!

From our community

Check out what members of our awesome community have made!

License

The source code in this repository is licensed under the MIT License. The MIT License covers the source code only. It does not cover the ChilliCream name and trademarks, the logos and package icons, or the website content and design. See TRADEMARKS.md for details.

Financial Contributors

Become a financial contributor and help us sustain our community.

Become a sponsor and get your logo on our README on Github with a link to your site.

Backer

Become a backer and get your image on our README on Github with a link to your site.

评论

暂无评论