【发布时间】:2019-03-06 16:41:24
【问题描述】:
我是 GraphQL 的新手。
目前,我有一个查询定义,用于搜索 students 及其参与的 classes:
var studentsQueryArguments = new QueryArguments();
studentsQueryArguments.Add(new QueryArgument<ListGraphType<IntGraphType>> { Name = "ids", Description = "Student indexes." });
studentsQueryArguments.Add(new QueryArgument<RangeModelType<double?, double?>> {Name = "age", Description = "Age range of student."});
Field<ListGraphType<StudentType>>(
"students",
arguments: studentsQueryArguments,
resolve: context =>
{
var students = relationalDbContext.Students.AsQueryable();
var classes = relationalDbContext.Classes.AsQueryable();
var participatedClasses = relationalDbContext.StudentInClasses.AsQueryable();
var ids = context.GetArgument<List<int>>("ids");
var age = context.GetArgument<RangeModel<double?, double?>>("age");
if (ids != null)
students = students.Where(x => ids.Contains(x.Id));
if (age != null)
{
var from = age.From;
var to = age.To;
if (from != null)
students = students.Where(x => x.Age >= from);
if (to != null)
students = students.Where(x => x.Age <= to);
}
var results = (from student in students
select new StudentViewModel
{
Id = student.Id,
Age = student.Age,
FullName = student.FullName,
Photo = student.Photo,
Classes = from participatedClass in participatedClasses
from oClass in classes
where participatedClass.StudentId == student.Id &&
participatedClass.ClassId == oClass.Id
select new ClassViewModel
{
Id = oClass.Id,
ClosingHour = oClass.ClosingHour,
Name = oClass.Name,
OpeningHour = oClass.OpeningHour
}
});
return results;
});
在上面的代码中,我加入了 Student 和 Class。 用查询
{
students(ids: [1, 2, 3]) {
id
age
classes {
name
openingHour
closingHour
}
}
}
学生和他们的班级被返回。没关系。
我想要的是当我使用这个查询时:
{
students(ids: [1, 2, 3]) {
id
age
}
}
我的应用不会将Student加入Class,而只会返回Student信息。
有可能吗?
谢谢,
【问题讨论】:
标签: c# graphql asp.net-core-2.0