簡體   English   中英

GraphQL 我將如何編寫查詢以按名字查找

[英]GraphQL How would I write a query to find by first name

我正在嘗試了解 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
  }
}

您將需要要么改變你在這里現場制作的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是不夠的,您還需要一個姓氏參數,並且它們是可選的,因此您可以執行以下操作:

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"));
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM