question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
artecoopcommented, Nov 4, 2019

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.

1reaction
matt-e-kingcommented, Nov 1, 2019

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

type Category @model
    @key(fields: ["seoName", "createdAt"])
    @key(name: "ByOrder", fields: ["seoName", "order"], queryField: "itemsByOrder")
{
    seoName: String!
    createdAt: String!
    name: String!
    description: String
    order: Int!
    deleted: Boolean
}

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 😃

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found