Query Table

When you want to query some data by a certain attribute, this section will help you.

  1. Select the Schema tab
  • Add the code below to the Query type to add a new query called allPostsByAuthor
    allPostsByAuthor(author: String!, count: Int, nextToken: String): PaginatedPosts!
  • Then click Save Schema

CreateAddPostResolver

  1. Next, in the Resolvers pane on the right, find the allPostsByAuthor field of type Query, then select Attach

CreateAddPostResolver

  1. On the Attach resolver configuration page:
  • Resolver type: Select Unit resolver
  • Resolver runtime: Select Velocity Template Language (VTL)
  • Data source name: Select PostDynamoDBTable
  • Click the orange Create button.
  1. On the code editor page of the newly created Resolver:
  • Delete the default code and paste the content below into the Configure the request mapping template section:
{
    "version" : "2017-02-28",
    "operation" : "Query",
    "index" : "author-index",
    "query" : {
      "expression": "author = :author",
        "expressionValues" : {
          ":author" : $util.dynamodb.toDynamoDBJson($context.arguments.author)
        }
    }
    #if( ${context.arguments.count} )
        ,"limit": $util.toJson($context.arguments.count)
    #end
    #if( ${context.arguments.nextToken} )
        ,"nextToken": "${context.arguments.nextToken}"
    #end
}
  • Scroll down, delete the default code, and paste the content below into the Configure the response mapping template section:
#if($context.error)
  $util.error($context.error.message, $context.error.type)
#end
{
    "posts": $utils.toJson($context.result.items)
    #if( ${context.result.nextToken} )
        ,"nextToken": $util.toJson($context.result.nextToken)
    #end
}
  • Click Save in the corner of the page to save

CreateAddPostResolver

  1. Select the Queries tab on the left menu
  • Paste the script below into the Queries section to add some new sample posts:
mutation addPost {
  post1: addPost(id:"10" author: "Nadia" title: "The cutest dog in the world" content: "So cute. So very, very cute." url: "https://aws.amazon.com/appsync/" ) { author, title }
  post2: addPost(id:"11" author: "Nadia" title: "Did you know...?" content: "AppSync works offline?" url: "https://aws.amazon.com/appsync/" ) { author, title }
  post3: addPost(id:"12" author: "Steve" title: "I like GraphQL" content: "It's great" url: "https://aws.amazon.com/appsync/" ) { author, title }
}
  • Then click the Run button (the orange Play button), select addPost (if given the option)

CreateAddPostResolver

  1. Delete the old code and paste the following query script into the Queries section:
query allPostsByAuthor {
  allPostsByAuthor(author: "Nadia") {
    posts {
      id
      title
    }
    nextToken
  }
}
  • Then click the Run button (the orange Play button), select allPostsByAuthor (if given the option). The result returned will be the posts by the author Nadia

CreateAddPostResolver

  1. Change the value of the author field to AUTHORNAME and add the parameter count: 5
  • Click the Run button (the orange Play button)

CreateAddPostResolver

  1. Copy the value of the nextToken field from the returned result and assign it as the input parameter for the query (similar to the previous step)
  • Click the Run button (the orange Play button), the result will return the data of the next page

CreateAddPostResolver