GraphQL 我将如何编写查询以按名字查找 [英] GraphQL How would I write a query to find by first name
问题描述
我正在尝试了解 Graph QL 并有一个基本的示例工作.例如如果我传入此查询,我将返回与 id 的匹配项.
I am trying to understand Graph QL and have a basic example working. e.g. if I pass in this query I get back the match with the id.
query {
person(id:"4090D8F6-EFC4-42CD-B55C-E2203537380C")
{
firstname
surname
}
}
我的数据只是一组静态的测试数据,我现在想要做的是返回名字与我提供的内容相匹配的所有用户.我很困惑如何写这个,因为 id null 检查似乎阻止了我!?
My data is just a static set of test data, what I would like to do now is return all users that have a first name matching what I provide. I am baffled how I would write this as the id null check seems to stop me!?
我的 PersonQuery 如下所示:
My PersonQuery Looks like this:
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 的帮助示例.
How would I make it so that I can return a list of persons by the first name, don't event know if this is a valid query below but would like some help on how I could do this along with the working id example.
query {
person
{
firstname: ("Andrew")
surname
}
}
答案更新 - DavidG 提供
我按照上面提到的做了,所以我的 PersonQuery 现在看起来像这样
I did as was mentioned so my PersonQuery now looks like this
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 查询:
I could then run the graphql query as follows:
query {
persons(firstname: "Andrew", surname: "P")
{
firstname
surname
}
}
推荐答案
您需要更改此处的字段以使 id
参数可选或创建一个新字段(可能称为 persons
或 people
) 并添加您解析到数据存储库中的新参数.就我个人而言,我更喜欢做后者并创造一个新领域.例如:
You would need to either change the field you have here to make the id
parameter optional or create a new field (maybe called persons
or people
) and add a new parameter that you parse into your data repository. Personally, I would favour doing the latter and create a new field. For example:
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
方法.查询现在看起来像这样:
And now you just need to write the GetByFirstName
method yourself. The query would now look like this:
query {
people(firstName:"Andrew")
{
firstname
surname
}
}
现在您可能会发现 GetByFirstName
是不够的,您还需要一个姓氏参数,并且它们是可选的,因此您可以执行以下操作:
Now you may find that GetByFirstName
isn't enough and you also want a surname parameter too, and for them to be optional, so you could do something like this:
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"));
});
这篇关于GraphQL 我将如何编写查询以按名字查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!