为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel Lighthouse GraphQL - 在服务器端排序

Laravel Lighthouse GraphQL - 在服务器端排序

PHP
德玛西亚99 2021-10-08 15:40:37
我是 GraphQL 的新手,我正在尝试根据列内容对我的数据进行排序。我有一个可以发送的查询端点:query {  user(count:20, firstName:"Foo") {    data {      name      region      birthDate    }  }}结果是一个包含 20 个用户名的数组Foo。但我想通过birthDate. 我已经尝试了很多东西,但我就是想不通。我已经尝试过在 firstName之前sort和orderBy之后,但我总是收到错误,例如:{  "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!}
查看完整描述

1 回答

?
慕仙森

TA贡献1827条经验 获得超8个赞

又找了一个小时,终于找到了。我将我的 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

    }

  }

}


查看完整回答
反对 回复 2021-10-08
  • 1 回答
  • 0 关注
  • 162 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信