How do I configure an AWS AppSync schema to handle nested JSON data in DynamoDB?

Last updated: 2021-01-11

I want my AWS AppSync schema to retrieve the response from an Amazon DynamoDB table that has nested JSON data. How can I do that?

Short description

Do the following to get an AWS AppSync schema to handle nested JSON data in DynamoDB:

  • Add a nested JSON data item to the DynamoDB table.
  • Create an AppSync API and attach the data source.
  • Configure the nested JSON schema in the AppSync API.
  • Attach a resolver to the getItems query.
  • Create a new test query.

Important: The AWS AppSync schema passes null values in its response to DynamoDB if the field names aren’t mapped to the nested JSON data.

Resolution

Add a nested JSON data item to the DynamoDB table

1.    Open the AWS DynamoDB console.

2.    Choose Create table.

3.    In the Table name field, enter a descriptive name.

4.    In the Primary key field, enter a field name. For example: id.

5.    Choose Create.

6.    Choose the Items tab and select Create item.

7.    From the upper left dropdown list in the Create item dialogue box, choose Text.

Note: Tree is the default selection for the dropdown list.

8.    Copy and paste the following nested JSON record in the text item and choose Save:

Important: Make sure to overwrite the prefilled content in the text box with the nested JSON record.

Example nested JSON record

{
    "id": "123",
    "product": {
        "model": {
            "property": {
                "battery": "li-ion",
                "device": "iOT-Device",
                "pressure": "1012",
                "up_time": "02:12:34"
            }
        }
    },
    "status": "In-Stock"
}

Note: For more information, see Create a table.

Create an AppSync API and attach the data source

1.    Open the AWS AppSync console.

2.    Choose Create API.

3.    On the Getting Started page, under Customize your API or import from Amazon DynamoDB, choose Build from scratch.

4.    Choose Start.

5.    In the API name field, enter a name for your API.

6.    Choose Create.

7.    In the left navigation pane, choose Data Sources.

8.    Choose Create data source.

9.    On the New Data Source page, under Create new Data Source, choose the following options: For Data source name, enter a descriptive name. For Data source type, choose Amazon DynamoDB table. For Region, choose the region that contains your DynamoDB table. For Table name, choose the table you just created.

Important: Leave all other options as default.

10.    Choose Create.

Note: For more information, see Attaching a data source.

Configure the nested JSON schema in the AppSync API

1.    In the left navigation pane of the AWS AppSync console, choose Schema.

2.    Copy and paste the following nested JSON schema into the text box and choose Save Schema:

Important: Make sure to overwrite the prefilled content in the text box with the nested JSON schema.

Example nested JSON schema

type Query {
    getItems(id: String!): allData
}

type allData {
    id: String!
    product: toModel
    status: String
}

type items {
    battery: String
    device: String
    pressure: String
    up_time: String
}

schema {
    query: Query
}

type toModel {
    model: toProperties
}

type toProperties {
    property: items
}

Note: For more information, see Designing your schema.

Attach a resolver to the getItems query

1.    In the AWS AppSync console, on the Schema page of your API, under Resolvers, scroll to Query. Or in the Filter types field, enter Query.

2.    Next to getItems(...): allData, under Resolver, choose Attach.

3.    On the Create new Resolver page, for Data source name, choose the name of the DynamoDB table you created earlier.

Important: Don't change the default mapping templates for the DynamoDB getItem operation.

4.    Choose Save Resolvers.

Example request mapping template

{
    "version": "2017-02-28",
    "operation": "GetItem",
    "key": {
        "ID": $util.dynamodb.toDynamoDBJson($ctx.args.ID),
    }
}

Example response mapping template

$util.toJson($ctx.result)

Note: For more information, see Configuring resolvers.

Create a new test query

1.    In the AWS AppSync console, in the left navigation pane, choose Queries.

2.    On the Queries page of your API, in the Query editor, copy and paste the following query:

Example test query

query getItem {
    getItems(id:"123") {
      id
      product{
          model{
            property{
            pressure 
            device
            battery
            up_time
          }
        }
      }
      status
    }
  }

3.    Choose the play icon or press Ctrl/Cmd + Enter to run the test query.

Note: The test query should return the following results:

Example test query results

{
  "data": {
    "getItems": {
      "id": "123",
      "product": {
        "model": {
          "property": {
            "pressure": "1012",
            "device": "iOT-Device",
            "battery": "li-ion",
            "up_time": "02:12:34"
          }
        }
      },
      "status": "In-Stock"
    }
  }
}

In this way, you can retrieve any nested JSON data from an Amazon DynamoDB table through AppSync GraphQL operations.


Did this article help?


Do you need billing or technical support?