代码之家  ›  专栏  ›  技术社区  ›  Andrew

如何编写按名字查找的查询

  •  1
  • Andrew  · 技术社区  · 5 年前

    我正在尝试理解图ql,并有一个基本的示例在工作。例如,如果我传递这个查询,我将得到与ID匹配的结果。

    query {
      person(id:"4090D8F6-EFC4-42CD-B55C-E2203537380C")
      {
        firstname
        surname
      }
    }
    

    我的数据只是一组静态的测试数据,现在我想做的是返回所有名字与我提供的名字匹配的用户。我很困惑怎么写这个,因为ID空支票似乎阻止了我!?

    我的个人查询如下:

    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提供

    我按上面提到的做了,所以我的个人查询现在看起来像这样

    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
      }
    }
    
    1 回复  |  直到 5 年前
        1
  •  4
  •   DavidG    5 年前

    你要么改变你在这里的领域,使 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"));
        });