mec111
mec111

Reputation: 15

AWS-Appsync How to query latest items from the dynamoDB table?

I have just gotten started with AWS Appsync and have gone through most of the tutorial (https://aws-amplify.github.io/docs/js/api) using the GraphQL and SDK methods mostly.

How do I query my dynamoDB table to only return the latest 50 articles?


class TestScreen extends Component {
    state = { list: [] }

    async componentDidMount() {
        const fetched = await API.graphql(graphqlOperation(queries.listArticles))
        this.setState({ 
            list: fetched.data.listArticles.items,
        })
    }

    renderRow(item) {
        return (
            <View>
                <Text>{item.name}</Text>
            </View>
        )
    }

    render() {
        return (
            <View>
                <FlatList
                    data={this.state.list}
                    renderItem={({item}) => this.renderRow(item)}
                    keyExtractor={item => item.id}
                />
            </View>
        )
    }
}

Currently the query returns me the list of names but not in any identifiable order.

Upvotes: 0

Views: 1307

Answers (1)

Neill
Neill

Reputation: 517

You can create a secondary index on a timestamp field and use it to query the table. In addition to that, you can also do pagination.

Upvotes: 1

Related Questions