Generating a GraphQL API From Your Database — AWS AppSync

May 26, 2019

How to fast-track your GraphQL API creation

This hands-on guide will hold your hands through each step as you let AWS create a GraphQL API for you using a DynamoDB table.

If you already have a DynamoDB table you want to use, skip ahead to the next section. If not, then go ahead and create a table of magical kittens:

  1. Go to Services > DynamoDB
  2. Click Create table.
  3. For Table name, enter: Magical-Kittens-Table
  4. For Primary key, enter: kitten_id
  5. Click Create.
  6. Then Create item.
  7. For Item {1} kitten-id, enter: brit-shorthair1
  8. Click on the + and Append a Number: { age : 0.5}
  9. Append a String: { personality : fierce }
  10. Append a Boolean: { magical: true } and click Save.
DynamoDB table in the AWS console

Voila! You now have a table. Let’s make that API now:

  1. Go to Services > AWS AppSync.
  2. Click Create API.
  3. Select Import DynamoDB table and Start.
  4. Choose the Magical-Kittens-Table or another table from the dropdown and Import.
  5. Keep all the defaults and Create and Create again.

And you’re done! AWS AppSync just created a GraphQL API for you. Let’s play with it:

  1. Change the "kitten_id" in the QUERY VARIABLES section at the bottom from "Hello, world!" to something more sensible, like "siberian1".
  2. Click the Play button and select the ‘create’ mutation from the dropdown. Congrats, your API just wrote a Siberian kitten to DynamoDB! 🦁
  3. You can verify the existence of the Siberian by clicking the Play button again and selecting the ‘list’ query from the dropdown. All your kittens show up on the right!

4. Click on Schema in the left-hand menu and you’ll see just how much work AWS AppSync did for you. Looks like it wrote 99 lines of code.

5. Click on the Export schema dropdown and select schema.graphql and Save it. You can now put it in version control.

You own the API that AWS made for you, so go ahead and export the schema.

6. Click on Data Sources on the sidebar. That’s where your DynamoDB table is referenced. 👀

Generating a schema.graphql from a DB = as easy as generating a cat from a Poké Ball

This was just a quick introduction to the fun and wonders of AWS AppSync. For a more in-depth overview of the service, my Serverless Guru colleague Luke Vandekieft has made a detailed video:

Serverless Handbook
Access free book

The dream team

At Serverless Guru, we're a collective of proactive solution finders. We prioritize genuineness, forward-thinking vision, and above all, we commit to diligently serving our members each and every day.

See open positions

Looking for skilled architects & developers?

Join businesses around the globe that trust our services. Let's start your serverless journey. Get in touch today!
Ryan Jones - Founder
Ryan Jones
Founder
Speak to a Guru
arrow
Edu Marcos - CTO
Edu Marcos
Chief Technology Officer
Speak to a Guru
arrow
Mason Toberny
Mason Toberny
Head of Enterprise Accounts
Speak to a Guru
arrow

Join the Community

Gather, share, and learn about AWS and serverless with enthusiasts worldwide in our open and free community.