在 @apollo/client 中对数据进行排序的功能

huangapple go评论81阅读模式
英文:

Sorting data in gql function from @apollo/client

问题

遇到了一个问题。该项目基于 NextJS 并使用 @apollo/client 库。我有一个查询如下:

export const GET_USERS = gql`
    query User($uid: Int){
        users(pagination: { limit: -1 }, sort: "guid desc, uid", filters: {uid: {eq: $uid}}){
            data{
            ....

其中 guiduid 是整数字段。
并且用户是按升序获取的,这是显而易见的。
我需要将排序改为降序,即从 guid 的最大值到最小值。

如何对 guid 字段进行降序排序?

尝试了不同的选项,如 sort: "guid desc, uid"sort: "guid(desc), uid"sort: "guid: desc, uid" 等等。尝试使用 sortssortByorderorderBy,但它们报错说未知参数。我尝试将一个对象放入 sort 中,但仍然报错,说需要字符串类型。

英文:

Faced a problem. The project is based on NextJS and uses the @apollo/client library. I have a query like:

export const GET_USERS = gql`
    query User($uid: Int){
        users(pagination: { limit: -1 }, sort: "guid, uid", filters: {uid: {eq: $uid}}){
            data{
            ....

where guid and uid are integer fields.
And Users are obtained in ascending order, which is obvious.
I need the sort to be in descending order, i.e. from the largest value in guid to the smallest.

How to sort in descending order for the guid field?

Tried different options and sort: "guid desc, uid" sort: "guid(desc), uid" sort: "guid: desc, uid" and so on. Tried to use sorts, sortBy, order, orderBy, swears at them that unknown arguments. I tried to put an object in sort, swears, says that the string type was expected.

答案1

得分: 0

export const GET_USERS = gql`
query User($uid: Int){
users(pagination: { limit: -1 }, sort: { guid: -1, uid: 1 }, filters: {uid: {eq: $uid}}){
data{
....

英文:

export const GET_USERS = gql`
query User($uid: Int){
users(pagination: { limit: -1 }, sort: { guid: -1, uid: 1 }, filters: {uid: {eq: $uid}}){
data{
....

答案2

得分: 0

解决方案原来非常巧妙简单:sort: "guid: desc, uid"选项是最接近的。正确的是:sort: "guid:desc, uid",即在***:***之后不需要空格。

英文:

The solution turned out to be ingeniously simple: The sort: "guid: desc, uid" option was the closest. Correct: sort: "guid:desc, uid", i.e. after : no space needed...

huangapple
  • 本文由 发表于 2023年7月23日 17:20:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定