Amplify-cli: Enable sorting on @connection

Created on 7 Sep 2018  路  3Comments  路  Source: aws-amplify/amplify-cli

Is your feature request related to a problem? Please describe.

take this schema:

type Conversation @model {
  id: ID!
  messages: [Message] @connection(name: "ConvoMsgs")
}

type Message @model {
  id: ID!
  conversation: Conversation @connection(name: "ConvoMsgs")
}

this produces

type Conversation {
  id: ID!
  messages(filter: ModelMessageFilterInput, sortDirection: ModelSortDirection, limit: Int, nextToken: String): ModelMessageConnection
}

messages resolver works by doing query on GSI with messageConversationId as the HASH key.
There is no RANGE key, so the sort has no impact (since resolver is using ScanIndexForward) and returned order is not guaranteed. This has a huge impact if we fetch messages in bunches. Think of a long conversation or a long todo list. the list order is important.

Describe the solution you'd like

Update the @connection directive to include a sortField

example

type Message
  @model(
    mutations: { create: "createMessage" }
    queries: null
    subscriptions: null
  ) {
  id: ID!
  conversation: Conversation
    @connection(name: "ConvoMsgs", sortField: "createdAt")
}

which creates:

{
...
  "GlobalSecondaryIndexes": [
    {
      "IndexName": "gsi-ConvoMsgs",
      "KeySchema": [
        {
          "AttributeName": "messageConversationId",
          "KeyType": "HASH"
        },
        {
          "AttributeName": "createdAt",
          "KeyType": "RANGE"
        }
      ],
...
  ]
...
}

If sortField is excluded, a RANGE key should not be created.
Adding the sortField does not impact the generated schema or resolver.
setting the createdAt or updatedAt attribute as RANGE could be interesting defaults.

Describe alternatives you've considered

Not sure there's an alternative here. any directions?

feature-request graphql-transformer work-in-progress

Most helpful comment

I get that this is a feature, but this is the single-handed reason we're not using Amplify in production.

If there's any sort of good work around, would love to hear it.

All 3 comments

I get that this is a feature, but this is the single-handed reason we're not using Amplify in production.

If there's any sort of good work around, would love to hear it.

@CodySwannGT This feature has been added with the merged PR above. We can close this issue.

I'm seeing issues with this after the new cloudformation fix:

UPDATE_ROLLBACK_IN_PROGRESS *******-20190213165923 AWS::CloudFormation::Stack Tue Feb 19 2019 17:42:40 GMT-0800 (PST) The following resource(s) failed to update: [api*******].

I added one line to sort notifications:

notifications: [Notification] @connection(name: "UserNotifications", sortField: "createdAt")
Was this page helpful?
0 / 5 - 0 ratings