5

嗨,我是 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!
}
4

1 回答 1

13

又找了一个小时,终于找到了。我将我的 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
    }
  }
}
于 2019-07-14T19:04:10.370 回答