Amplify-cli: Reverse sort direction with sortField

Created on 17 Jun 2019  路  7Comments  路  Source: aws-amplify/amplify-cli

* Which Category is your question related to? *
graphql transform

* What AWS Services are you utilizing? *
Amplify api

* Provide additional details e.g. code snippets *
How can I make the sortField sort in reverse order? For instance, the schema below returns the oldest Comment first when using getPost on a given Post. I want it to return the most recently created Comment first, using the createdAt String.

Schema example:

type Post @model {
    id: ID!
    title: String!
    comments: [Comment] @connection(name: "PostComments", sortField: "createdAt")
}
type Comment @model {
    id: ID!
    content: String!
    post: Post @connection(name: "PostComments", sortField: "createdAt")
    createdAt: String
}

query example:
getPost(id:"xxxxxxx") {
    comments {
        items {
             id
             content
        }
        nextToken
    }
}
enhancement graphql-transformer pending-response

Most helpful comment

Hi @aehringer based on your current schema the generated type Post should include the following.

type Post {
  id: ID!
  title: String!
  comments(createdAt: ModelStringKeyConditionInput, filter: ModelCommentFilterInput, sortDirection: ModelSortDirection, limit: Int, nextToken: String): ModelCommentConnection
}

By specifying the sortDirection you should be able to get the results in reverse.

query GetPost($id: ID!) {
  getPost(id: $id) {
    comments(sortDirection: DESC) {
      items {
        id
        content
        createdAt
      }
      nextToken
    }
  }
}

All 7 comments

@aehringer We don't support it out of the box as a part of our transformer at this moment, but i've marked this as an enhancement and we're working on it currently.

Hi @aehringer based on your current schema the generated type Post should include the following.

type Post {
  id: ID!
  title: String!
  comments(createdAt: ModelStringKeyConditionInput, filter: ModelCommentFilterInput, sortDirection: ModelSortDirection, limit: Int, nextToken: String): ModelCommentConnection
}

By specifying the sortDirection you should be able to get the results in reverse.

query GetPost($id: ID!) {
  getPost(id: $id) {
    comments(sortDirection: DESC) {
      items {
        id
        content
        createdAt
      }
      nextToken
    }
  }
}

We have the same issue with @key.
The default sort order on createdAt is "ASC" and can't be changed.
type Order @model @key(fields: ["customerEmail", "createdAt"]) { customerEmail: String! createdAt: String! orderId: ID! }

Query
query ListOrdersForCustomerIn2019 { listOrders(customerEmail:"[email protected]", createdAt: { beginsWith: "2019" }) { items { orderId customerEmail createdAt } } }
This will return the oldest instead of latest orders, which isn't helpful in a real scenario. Fresh data is what we crave for.

+1

Most applications require their data sorted in DESC order according to createdAt.
Definitely should be a native option.

@julescsv I've opened a pr, #1990, where you can specify the sort direction like the following

query ListOrdersForCustomerIn2019 { 
    listOrders(
    customerEmail:"[email protected]",
    createdAt: { beginsWith: "2019" }
    sortDirection: DESC
    ) { 
    items { 
        orderId
        customerEmail
        createdAt 
        } 
    } 
}

Closing issue as pr has been merged

Really sorry to resurrect this @SwaySway, @akshbhu but has the use case of sorting on a GET query been touched upon or is it just LIST?
I have a user model and in there I have a connection on orders.
orders: [Order] @connection(keyName: "byUser", fields: ["id"], sortField: "date")
I run a getUser which returns the oldest 10 orders rather than the latest 10.
I checked the docs and issues but I could only find solutions for LIST.
Thanks for your help.

Was this page helpful?
0 / 5 - 0 ratings