我试图理解Graph QL,并有一个基本的示例工作。例如如果我在此查询中传递,则会获得ID为ID的匹配项。
query {
person(id:"4090D8F6-EFC4-42CD-B55C-E2203537380C")
{
firstname
surname
}
}
我的数据只是测试数据的静态集合,我现在想做的是返回所有名字与我提供的名字匹配的用户。我不知如何写,因为id null检查似乎阻止了我!?
我的PersonQuery看起来像这样:
public class PersonQuery : ObjectGraphType<Person>
{
public PersonQuery(ShoppingData data)
{
Field<PersonType>(
"person",
description: "A Person",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<IdGraphType>>
{
Name = "id",
Description = "The id of the person"
}),
resolve: ctx =>
{
return data.GetById(ctx.GetArgument<Guid>("id"));
});
}
}
我将如何做到这一点,以便我可以按名字返回一个人的名单,不知道下面是否这是一个有效的查询,但是想要一些有关如何使用工作ID的帮助例子。
query {
person
{
firstname: ("Andrew")
surname
}
}
答案更新-由DavidG提供
我按照提到的那样做,所以我的PersonQuery现在看起来像这样
public class PersonQuery : ObjectGraphType<Person>
{
public PersonQuery(ShoppingData data)
{
Field<PersonType>(
name: "person",
description: "A Person",
arguments: new QueryArguments(
new QueryArgument<IdGraphType>
{
Name = "id",
Description = "The id of the person"
}),
resolve: ctx =>
{
return data.GetById(ctx.GetArgument<Guid>("id"));
});
Field<ListGraphType<PersonType>>(
name : "persons",
description: "Persons",
arguments: new QueryArguments(
new QueryArgument<StringGraphType>
{
Name = "firstname",
Description = "The firstname of the person"
},
new QueryArgument<StringGraphType>
{
Name = "surname",
Description = "The surname of the person"
}),
resolve: ctx =>
{
var firstName = ctx.GetArgument<String>("firstname");
var surname = ctx.GetArgument<String>("surname");
return data.Filter(firstName, surname);
});
}
}
然后我可以如下运行graphql查询:
query {
persons(firstname: "Andrew", surname: "P")
{
firstname
surname
}
}
答案 0 :(得分:4)
您需要更改此处的字段以使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
}
}
现在您可能会发现GetByFirstName
是不够的,并且您还想要一个姓氏参数,并且它们是可选参数,因此您可以执行以下操作:
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"));
});