【问题标题】:Laravel Lighthouse GraphQL - Sorting on server sideLaravel Lighthouse GraphQL - 在服务器端排序
【发布时间】:2019-11-23 11:37:34
【问题描述】:

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

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

结果是一个包含 20 个名字为 Foo 的用户的数组。但我想通过birthDate 订购它们。我已经尝试了很多东西,但我就是想不通。我已经尝试在名字之后添加 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!
}

【问题讨论】:

    标签: php laravel graphql laravel-lighthouse


    【解决方案1】:

    又找了一个小时,终于找到了。我将我的 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-01-06
    • 2022-07-12
    • 2021-10-22
    • 1970-01-01
    • 2023-02-13
    • 2019-12-22
    • 2020-10-03
    • 1970-01-01
    • 2021-07-13
    相关资源
    最近更新 更多