How to create secondary indexes with GraphQL and AWS AppSync

This post assumes you have a general understanding of GraphQL, AppSync, and DynamoDB.

Phil Andrews
Sep 16, 2019 · 2 min read

When working with AppSync you’re usually tied to DynamoDB as a data store. This can be tricky due to the nature of NoSQL. Figuring out your data access patterns early will help you down the line. But if you don’t it’s relatively easy to update your schema on the fly.

For instance, if you realize that you will actually need to query by a particular field on a regular basis and that field isn’t the hash key you’ll need a global secondary index (GSI). This is standard stuff in DynamoDB but is abstracted when we move up the stack to AppSync, which handles the majority of indexing and provisioning for you behind the scenes.

So if you find yourself in need of a GSI for a table that has already been created go to your schema.graphql file.

Within the model that you want to create an index on we’ll add the keyword key to our type. Changing this…

To this…

What this says to AppSync is that you want to create a GSI on the homeTeamId field. When you run amplify push it will be done for you automatically. Once your publish is done you can start to use that GSI by running a GraphQL query on the queryField we specified, boxScoresByTeam.

Things To Note

  1. You can only create one GSI per amplify push . If you try to run more than one the CLI will throw an error and rollback any updates,
  2. GSI’s cost money. They’re provisioned throughput so make sure you actually need one before creating it. This goes back to having a thorough understanding of your schema before spinning up resources.
  3. You need to create the GSI through the AppSync CLI or dashboard otherwise your generated GraphQL schema won’t recognize the tables new index and query capabilities. Which means you’ll get errors when trying to query using the new queryField.

To read through all the available directives with AppSync GraphQL you can check out the docs.

Want your console.logs back when developing serverless? Checkout Twig…

Get smarter at building your thing. Join The Startup’s +800K followers.

Phil Andrews

Written by

The Startup
Phil Andrews

Written by

Always be improving : engineer : : ig-phlndrws

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store