【发布时间】:2020-12-14 21:03:35
【问题描述】:
我正在尝试在 C# 中实现一个 graphql api。我有基本的工作,但我正在努力让嵌套查询工作。
我已经看到它在 NodeJS 等中实现。我只是想知道是否有人可以帮助我在 c# 中实现相同的功能。
基本类型:
public AirportType()
{
Name = "Airport";
Field(x => x.Id, type: typeof(IdGraphType)).Description("The ID of the Airport.");
Field(x => x.Name).Description("The name of the Airport");
Field(x => x.Location).Description("The Location of the Airport");
Field(x => x.Plane,nullable:true, type: typeof(ListGraphType<PlaneType>)).Description("Aiports Planes");
}
public PlaneType()
{
Name = "Plane";
Field(x => x.Id, type: typeof(IdGraphType)).Description("The ID of the Plane.");
Field(x => x.Model).Description("The model of the Plane");
Field(x => x.Callsign).Description("The callsign of the Plane");
Field(x => x.AirportId,nullable:true).Description("The parent Aiport");
Field(x => x.Pilot,nullable:true, type: typeof(ListGraphType<PilotType>)).Description("The Planes Pilots");
}
public PilotType()
{
Name = "Pilot";
Field(x => x.Id, type: typeof(IdGraphType)).Description("The ID of the Pilot.");
Field(x => x.Name).Description("The name of the Pilot");
Field(x => x.Surname).Description("The surname of the Pilot");
Field(x => x.PlaneId,nullable: true).Description("The parent Plane");
}
以及基本查询:
Field<AirportType>(
"airport",
arguments: new QueryArguments(
new QueryArgument<IdGraphType> { Name = "id", Description = "The ID of the aiport." }),
resolve: context =>
{
var id = context.GetArgument<int?>("id");
var airport = db.Airport.Include("Plane.Pilot").FirstOrDefault(i => i.Id == id);
return airport;
});
Field<ListGraphType<AirportType>>(
"airports",
resolve: context =>
{
var airports = db.Airport.Include("Plane.Pilot");
return airports;
});
Field<ListGraphType<PlaneType>>(
"planes",
resolve: context =>
{
var planes = db.Plane.Include("Pilot").Include("Airport");
return planes;
});
【问题讨论】:
-
究竟是什么不在这里工作?
-
查询有效,但我只能查询根,我不能查询子实体,例如 Airport(id: 1){ Plane(id:1)} ,我只能查询机场,不能添加平面子查询
-
但是出现错误无法查询?还是图表设置不正确?
-
我认为设置不正确
-
从上面的代码很难看出,但我的项目中有一些类似的代码。我不像你那样设置图表,我使用类似
Field<ListGraphType<PlaneType>>("Plane", "Aiports Planes");的代码。你的架构看起来好吗?