amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Reverse sort direction with sortField

Open aehringer opened this issue 7 years ago • 8 comments

** 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
    }
}

aehringer avatar Jun 17 '19 13:06 aehringer

@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.

kaustavghosh06 avatar Jun 20 '19 17:06 kaustavghosh06

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
    }
  }
}

SwaySway avatar Jun 21 '19 00:06 SwaySway

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.

julescsv avatar Jun 29 '19 05:06 julescsv

+1

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

rawadrifai avatar Jun 29 '19 22:06 rawadrifai

@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 
		} 
	} 
}

SwaySway avatar Aug 07 '19 18:08 SwaySway

Closing issue as pr has been merged

SwaySway avatar Aug 09 '19 18:08 SwaySway

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.

amuresia avatar Apr 22 '20 23:04 amuresia

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 26 '21 10:05 github-actions[bot]