type User {
username: String! ## Primary key
posts: [Post]
following: [Following]
}
type Following {
follower: String! ## Primary key
username: String! ## Sort key
posts: [Post]
}
type Post {
username: String! ## Primary key
postID: ID!
title: String
content: String
}
type Query {
getPostsByUsername(username: String!): User
}
schema {
query: Query
}
User.posts resolver:
{
"version": "2017-02-28",
"operation": "Query",
"query": {
"expression": "username = :username",
"expressionValues": {
":username": $util.dynamodb.toDynamoDBJson($context.source.username)
}
}
}
$util.toJson($ctx.result.items)
User.following resolver
{
"version": "2017-02-28",
"operation": "Query",
"query": {
"expression": "follower = :follower",
"expressionValues": {
":follower": $util.dynamodb.toDynamoDBJson($context.source.username)
}
}
}
$util.toJson($ctx.result.items)
Подписан.сообщения resolver
{
"version": "2017-02-28",
"operation": "Query",
"query": {
"expression": "username = :username",
"expressionValues": {
":username": $util.dynamodb.toDynamoDBJson($context.source.username)
}
}
}
$util.toJson($ctx.result.items)
Query.getPostsByUsername resolver
{
"version": "2017-02-28",
"operation": "GetItem",
"key": {
"username": $util.dynamodb.toDynamoDBJson($ctx.args.username),
}
}
$util.toJson($ctx.result)
Тестирование
UserTable
PostTable
FollowTable
Теперь мы можем запросить всесообщения от имени пользователя и сообщений всех друзей.
Надеюсь, что это полезно:)