【问题标题】:GraphQL How would I write a query to find by first nameGraphQL我如何编写查询以按名字查找
【发布时间】:2019-06-23 22:13:32
【问题描述】:

我正在尝试了解 Graph QL 并提供一个基本示例。例如如果我传入此查询,我将返回与 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
  }
}

【问题讨论】:

    标签: c# graphql


    【解决方案1】:

    您需要更改此处的字段以使 id 参数可选,或者创建一个新字段(可能称为 personspeople)并添加一个您解析到数据存储库中的新参数.就个人而言,我更倾向于做后者并创建一个新领域。例如:

    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 还不够,您还需要一个 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"));
        });
    

    【讨论】:

    • 谢谢你,我正在寻找什么,非常感谢
    猜你喜欢
    • 2017-12-23
    • 2017-01-31
    • 1970-01-01
    • 2021-04-20
    • 2018-05-31
    • 2012-05-13
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多