How to create Index via AWS Amplify GraphQL Schema?

Soni Pandey
3 min readFeb 11, 2020

AWS Amplify is a development platform for building secure, scalable mobile and web applications. Thanks to AWS Amplify which has saved a good amount of time who wanted to do development at our fingertips, it has provided the environment and automated the process in the background, so we are able to setup AWS services easily.

See what community says about AWS amplify on twitter —

In this article, we’ll learn about -
1. How can we kick off the project using amplify?
2. How can we add schema using amplify?
3. How can we create partition key and sort key in the schema?
4. How can we create indexes in the schema?

Prerequisites —

Follow these steps if your machine is not having any of these -

i) Install Node.js® and npm.

ii) Install Amplify CLI -

npm install -g @aws-amplify/cli

iii) Sign up for an AWS account.

iv) Configure AWS CLI.

  1. How can we kick off the project using amplify?

For setting up the project first, run this command and hit enter -

amplify init

and then answer all questions in a similar way -

After following these steps then your project has been successfully initialized and connected to the cloud and your console will look like this -

2. How can we add schema using amplify?

After initialization of the project, for adding schema to our project we need to run -

amplify add api

Adding api this will ask some questions.

We can change the schema.graphql file as per the requirement. Let’s take an example of employee schema -

type Employee @model {
id: ID!
email: String!
username: String
companyId: ID!
}
type Company @model{
id: ID!
name: String!
type: String
}

In the above example, Employee is having the primary key of id and Company is having the same id as a primary key.

Our primary key could be a combination of partition key + sort key. So let’s assume Employee is having the primary key of two fields companyId and id, our schema will look like -

type Employee @model @key(fields: ["companyId", "id"]){
companyId: ID!
id: ID!
email: String!
username: String
}

3. How can we create indexes in AWS amplify schema?

type Employee @model @key(name: "companyId-email-index", fields: ["companyId", "email" ], queryField: "employeeByCompanyIdAndEmail"){
id: ID!
email: String!
username: String
companyId: ID!
}
type Company @model{
id: ID!
name: String!
type: String
}

for adding indexes we need to write this -

@key(name: "companyId-email-index", fields: ["companyId", "email" ], queryField: "employeeByCompanyIdAndEmail")

Indexes can be added while creating the resource only (dynamoDB table) otherwise it will throw an error.

4. How to push new changes to AWS resources?

Once schema looks good then run this command and hit enter -

amplify push

This will ask some questions in this manner -

This will create respective queries and mutations. It will create relevant resources also into AWS - AWS AppSync, create respective dynamoDB table etc.. Check out the GitHub repository for implementations.

Conclusion

AWS Amplify has saved a good amount of time who wanted to do development at our fingertips, it has provided the environment and automated the process in the background, so we are able to setup AWS services easily.

Hope you find the article useful.

--

--

Soni Pandey

I am a Node.js Developer and eager to learn new technology. I blog, tweet & read whenever I can.