簡體   English   中英

Laravel Lighthouse GraphQL-在服務器端排序

[英]Laravel Lighthouse GraphQL - Sorting on server side

嗨,我是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的包裝器。 令我驚訝的是,我找不到有關如何執行此操作的任何信息。

我的查詢:

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
}

現在,使用此查詢將按birthDate正確對它們進行排序:

query {
  user(count:20, firstName:"Foo", orderBy: [
        {
          field: "birthDate"
          order: DESC
        }
    ]) {
    data {
      name
      region
      birthDate
    }
  }
}

暫無
暫無

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

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