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

5 minute read
0

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

To get an AWS AppSync schema to handle nested JSON data in DynamoDB, do the following:

  • Add a nested JSON data item to the DynamoDB table.
  • Create an AWS AppSync API and attach the data source.
  • Configure the nested JSON schema in the AWS 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 Amazon DynamoDB console.

2.    Choose Create table.

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

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

5.    Choose Create table. The new table appears on the console's Tables page.

6.    In the Name column, choose the new table's name. The table's Overview page opens.

7.    Select the Actions dropdown list. Then, choose Create item. The Create item page opens.

8.    Choose the JSON button.

9.    Copy and paste the following nested JSON record into the JSON editor, and then choose Save:

Important: Make sure that you overwrite the prefilled content in the JSON editor 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"
}

For more information, see Create a table.

Create an AWS 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.

For more information, see Attaching a data source.

Configure the nested JSON schema in the AWS AppSync API

1.    Open the AWS AppSync console.

2.    In the left navigation pane, choose Schema.

3.    Copy and paste the following nested JSON schema into the JSON editor, and then choose Save Schema:

Important: Make sure that you overwrite the prefilled content in the JSON editor 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
}

For more information, see Designing your schema.

Attach a resolver to the getItems query

1.    Open the AWS AppSync console.

2.    On the Schema page of your API, under Resolvers, scroll to Query.
Note: Or, in the Filter types field, you can enter Query.

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

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

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

5.    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)

For more information, see Configuring resolvers.

Create a new test query

1.    Open the AWS AppSync console.

2.    In the left navigation pane, choose Queries.

3.    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
    }
  }

4.    To run the test query, choose the play icon -or- press Ctrl/Cmd + Enter.

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"
    }
  }
}

You can now retrieve any nested JSON data from an Amazon DynamoDB table through AWS AppSync GraphQL operations.


Related information

Supported data types and naming rules in Amazon DynamoDB

Setting up the getPost resolver (DynamoDB GetItem)