gpt4 book ai didi

php - Laravel Lighthouse GraphQL - 在服务器端排序

转载 作者:行者123 更新时间:2023-12-04 23:39:31 26 4
gpt4 key购买 nike

嗨,我是 GraphQL 的新手,我正在尝试根据列内容对我的数据进行排序。我有一个可以发送的查询端点:

query {
user(count:20, firstName:"Foo") {
data {
name
region
birthDate
}
}
}

结果是一个由 20 个用户组成的数组,名字为 Foo 。但我想通过 birthDate 订购它们。我已经尝试了很多东西,但我就是想不通。我已经尝试在 firstName 之后添加 sortorderBy,但我总是遇到错误,例如:
{
"errors": [
{
"message": "Unknown argument \"sort\" on field \"user\" of type \"Query\".",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 2,
"column": 31
}
]
}
]
}

我使用 Laravel Lighthouse 作为 GraphQL 的 wapper。我很惊讶我找不到有关如何执行此操作的任何信息。

我的查询:
type Query {
user(firstName: String @eq): [User!]! @paginate(type: "paginator" model: "App\\User")
}

type User {
id: ID!
firstName: String!
lastName: String!
birthDate: DateTime!
email: String!
created_at: DateTime!
updated_at: DateTime!
}

最佳答案

又找了一个小时,终于找到了。我将我的 graphql 文件更新为:

type Query {
user(firstName: String @eq orderBy: [OrderByClause!] @orderBy): [User!]! @paginate(type: "paginator" model: "App\\User")
}

type User {
id: ID!
firstName: String!
lastName: String!
birthDate: DateTime!
email: String!
created_at: DateTime!
updated_at: DateTime!
}

input OrderByClause{
field: String!
order: SortOrder!
}

enum SortOrder {
ASC
DESC
}

现在使用此查询将按出生日期正确排序它们:
query {
user(count:20, firstName:"Foo", orderBy: [
{
field: "birthDate"
order: DESC
}
]) {
data {
name
region
birthDate
}
}
}

关于php - Laravel Lighthouse GraphQL - 在服务器端排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57028362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com