跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

C#中GraphQL查询语言_C#HotChocolate查询教程【高级】

HotChocolate 是构建 GraphQL 服务的框架,需编写 resolver 方法而非执行 GraphQL 字符串;resolver 要求 public、参数名匹配、支持 IQueryable 优化;返回 null 默认静默,非空字段才报错;UseRouting() 必须在 MapGraphQL() 前;schema 依赖命名约定。 HotChocolate 在 C# 中不是“支持 GraphQL 查询语言”的工具,而是帮你定义、暴露、执行 GraphQL 服务的框架——你写的
query
或
mutation
文本本身不运行在 C# 里,它被 HotChocolate 解析后,才调用你写的 C# resolver 方法。 怎么写一个能被 HotChocolate 执行的 query resolver 你不能直接在 C# 里拼接或执行 GraphQL 字符串(比如
"{ user(id: 1) { name } }"
),那只是请求载荷。真正要写的是 resolver —— 一个返回数据的 C# 方法,由 HotChocolate 在收到合法 query 后自动调用。 resolver 必须是 public 实例方法或静态方法,返回可序列化的类型(如
User
、
IQueryable
、
Task
) 参数名需与 query 中字段参数名一致(如
id
),HotChocolate 自动绑定;支持
IResolverContext
、
CancellationToken
等特殊参数 如果返回
IQueryable
,HotChocolate 会自动应用
@skip
、
@include
、字段选择(field selection)等优化,但前提是启用了
AddGraphQLServer().AddProjections()
别在 resolver 里手动解析
context.Request.Query
字符串——那是 HTTP 层的事,HotChocolate 已帮你做完 AST 解析和变量注入 为什么
GetUserById
resolver 返回 null 却没报错 HotChocolate 默认对 nullable 引用类型字段(如
string
、
User
)不做非空校验,即使 resolver 返回
null
,也会静默渲染为 JSON 中的
null
,而不是抛出
ExecutionError
。 C知道 CSDN推出的一款AI技术问答工具 下载 若字段在 schema 中声明为非空(
User!
),而 resolver 返回
null
,HotChocolate 才会抛出
"Cannot return null for non-nullable field"
错误 启用严格模式:在配置时加
.ModifyOptions(o => o.RemoveUnreachableTypes = true)
并确保所有类型标注正确(如
User?
vs
User
) 更可靠的做法是在 resolver 内主动抛异常:
if (user == null) throw new ExecutionError($"User {id} not found");
注意:EF Core 的
FirstOrDefaultAsync()
返回
null
是正常行为,不等于逻辑错误,是否报错取决于业务语义
UseRouting()
和
AddGraphQLServer()
的顺序不能颠倒 HotChocolate 要求 ASP.NET Core 的端点路由必须先注册,否则
MapGraphQL()
会找不到路由容器,启动时报
"No endpoint was configured for GraphQL"
或 404。 正确顺序:
app.UseRouting();
→
app.UseAuthentication();
→
app.UseAuthorization();
→
app.UseEndpoints(endpoints => endpoints.MapGraphQL());
AddGraphQLServer()
是
IServiceCollection
扩展,必须在
Program.cs
的
builder.Services
阶段调用,且要在
AddControllers()
之后、
AddEndpointsApiExplorer()
之前(若用 Minimal Hosting,需用
builder.AddGraphQLServer()
) 常见坑:把
MapGraphQL()
放在
UseStaticFiles()
之后但没加
UseRouting()
,结果 GraphQL endpoint 完全不响应 最易被忽略的一点:HotChocolate 的 schema 生成依赖命名约定和泛型推导,比如把 resolver 方法命名为
GetUsers
会自动映射到
users
字段,但若你写了
FetchAllUsers
又没显式用
[GraphQLName("users")]
,这个字段就不会出现在 schema 里——连 playground 都看不到,更别说查询了。

相关文章