GraphQL如何编写查询以按名字查找 [英] GraphQL How would I write a query to find by first name
问题描述
我试图理解Graph QL,并有一个基本的示例工作.例如如果我在此查询中传递,则会获得ID为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
参数为可选,或创建一个新字段(可能称为 person
或 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
是不够的,并且还需要一个surname参数,并且它们是可选参数,因此您可以执行以下操作:
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屋!