你要么改变你在这里的领域,使
id
参数可选或创建新字段(可以调用
persons
或
people
)并添加一个新的参数,将其解析到数据存储库中。就我个人而言,我更愿意做后者,并创造一个新的领域。例如:
public PersonQuery(ShoppingData data)
{
Field<PersonType>( /* snip */ );
//Note this is now returning a list of persons
Field<ListGraphType<PersonType>>(
"people", //The new field name
description: "A list of people",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>>
{
Name = "firstName", //The parameter to filter on first name
Description = "The first name of the person"
}),
resolve: ctx =>
{
//You will need to write this new method
return data.GetByFirstName(ctx.GetArgument<string>("firstName"));
});
}
现在你只需要写
GetByFirstName
自己动手。现在查询如下所示:
query {
people(firstName:"Andrew")
{
firstname
surname
}
}
现在你可能会发现
获得第一名
还不够,您还需要一个姓氏参数,并且这些参数是可选的,因此您可以这样做:
Field<ListGraphType<PersonType>>(
"people",
description: "A list of people",
arguments: new QueryArguments(
new QueryArgument<StringGraphType>
{
Name = "firstName", //The parameter to filter on first name
Description = "The first name of the person"
},
new QueryArgument<StringGraphType>
{
Name = "surname",
Description = "The surname of the person"
}),
resolve: ctx =>
{
//You will need to write this new method
return data.SearchPeople(
ctx.GetArgument<string>("firstName"),
ctx.GetArgument<string>("surame"));
});