Simple sorting on GraphQL model
See original GitHub issue** Which Category is your question related to? ** GraphQL schema
** What AWS Services are you utilizing? ** amplify, appsync, dynamodb
** Provide additional details e.g. code snippets ** Consider this schema:
type Category @model @key(fields: ["seoName"]) {
seoName: String!
name: String!
description: String
order: Int!
deleted: Boolean
}
I want to list all my categories ordered by the order
field.
If I modify the key as @key(fields: ["seoName", "order"])
and I specify sortDirection in the query, the ordering does not appear to work.
I even tried to add a second @key
condition like
type Category @model @key(fields: ["seoName"]) @key(name: "ByOrder", fields: ["seoName", "order"], queryField: "itemsByOrder") {
...
}
But i got an error:
InvalidDirectiveError: Invalid @key "ByOrder". You may not create a @key where the first field in 'fields' is the same as that of the primary @key unless the primary @key has multiple 'fields'. You cannot have a local secondary index without a sort key in the primary index.
I’m rather new to graphql but this is driving me crazy.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
GraphQL Filtering, Pagination & Sorting Tutorial with JavaScript
With Prisma, it is possible to return lists of elements that are sorted (ordered) according to specific criteria. For example, you can order...
Read more >Your First Graphql Api Sorting - StarTutorial
GraphQL Extensions provides reusable classes for sorting similar to pagination. First step is to add available sort parameters to the ArticlesConnection field.
Read more >GraphQL query with sorting by date - JavaScript - Amplify Docs
In this guide you will learn how to implement sorting in a GraphQL API. In our example, you will implement sorting results by...
Read more >Sorting GraphQL results - Fauna Documentation
You can sort GraphQL query results using an index and a user-defined function. The following example uses the following components: ... The GraphQL...
Read more >Postgres: Sort query results | Hasura GraphQL Docs
Results from your query can be sorted by using the order_by argument. The argument can be used to sort nested objects too. The...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yo @matt-e-king, as you can see, there are few other people waiting for this answer that, albeit simple, not so straightforward. I found more struggle working on amplify and graphql than with other projects, as I admit I’m pretty new on serverless and grapqhl. Let’s wait an official response.
@artecoop if you are going to declare both your primary key and a secondary index, you have to turn your primary key into a composite primary key by provide a sort key, like this:
In the snippet above notice I added “createdAt” as an example.
That said, even after getting getting rid of that error I cannot get a successful simple ordering to work either. I recently submitted this issue detailing my struggles: https://github.com/aws-amplify/amplify-cli/issues/2634
So I guess I’m kind of in the same boat as you still 😃