Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Todos tutorial step - sort Todos by createdAt date #478

Open
2 tasks
JoshuaTyler-GitHub opened this issue Sep 10, 2024 · 3 comments
Open
2 tasks
Labels
documentation Improvements or additions to documentation feature-request New feature or request GraphQL pending-maintainer-response

Comments

@JoshuaTyler-GitHub
Copy link

Is this related to a new or existing framework?

React, Next.js

Is this related to a new or existing API?

GraphQL API, New API

Is this related to another service?

No response

Describe the feature you'd like to request

I would like the tutorial starter project to be expanded on a bit to cover how Todos are sorted by the createdAt timestamp.

https://docs.amplify.aws/nextjs/start/quickstart/nextjs-app-router-client-components/

Describe the solution you'd like

I would like code examples of how to sort Todos by the time at which they were created.

Describe alternatives you've considered

You can sort them on the frontend but that does not do you any good if the newest Todos are not delivered to you from the backend.

Additional context

No response

Is this something that you'd be interested in working on?

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change
@JoshuaTyler-GitHub JoshuaTyler-GitHub changed the title Todos tutorial step: sort Todos by createdAt date Feature Request: Todos tutorial step - sort Todos by createdAt date Sep 10, 2024
@cwomack cwomack added GraphQL feature-request New feature or request documentation Improvements or additions to documentation and removed pending-triage labels Sep 10, 2024
@cwomack
Copy link
Member

cwomack commented Sep 11, 2024

Hello, @JoshuaTyler-GitHub and thanks for opening this issue. I've created a docs issue on the amplify-docs repo to have this considered and will review this request with our team internally. If there's any further context we need or questions we have, we'll follow up. Thanks!

@tomphan
Copy link

tomphan commented Jan 2, 2025

Hi. I would not need a feature request, but please could the team suggest how to list the Todo and sort by createdAt.
I tried & searched any available similar issue on the Internet but no luck.

As I see in other issue thread, we could create a secondaryIndexes with sortKeys(['createdAt']), however I cannot call the generated client query because this case we need to specify the exact field content value.

    Todo: a.model({
      content: a.string(),
      isDone: a.boolean(),
      createdAt: a.datetime(),
    }).secondaryIndexes((index) => [
      index('content').sortKeys(['createdAt']),      
    ]), 
          const { data: items, errors } = await client_schema.models.Todo.listTodoByContentAndCreatedAt(  
            {   
                content: "...", // ? <---- we need to specify
            },
            {
                sortDirection: 'DESC',
            }
        );

So, please could the anyone suggest how to sort a list with Gen 2? Thanks.

@tomphan
Copy link

tomphan commented Jan 2, 2025

Solved: After looking at the Gen 1 doc, for reference, I'm able to do that for Gen 2.
https://docs.amplify.aws/gen1/react/build-a-backend/graphqlapi/best-practice/query-with-sorting/

The trick is creating a new dummy field type with a fixed value Todo so all query with type: 'Todo' can return all records.

    Todo: a.model({
      content: a.string(),
      isDone: a.boolean(),
      createdAt: a.datetime(),
      type: a.string(),            // <------------ new field
    }).secondaryIndexes((index) => [
      index('type').sortKeys(['createdAt']),      // <-------- sort key
    ]),  

Then, populate / update all todos with new type field valued Todo, for querying.

          const { data: items, errors } = await client_schema.models.Todo.listTodoByTypeAndCreatedAt(  
            {   
                type: "Todo", //  <---------- populate all records contains value "Todo"
            },
            {
                sortDirection: 'DESC',
            }
        );

Anyway, please update the doc for Gen 2 so people like me will not need to adapt this trick from Gen 1. Thx.

@stocaaro stocaaro transferred this issue from aws-amplify/amplify-js Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature-request New feature or request GraphQL pending-maintainer-response
Projects
None yet
Development

No branches or pull requests

3 participants