Я работаю над приложением простых сообщений с AWS AppSync. Это моя схема:
type Conversation {
id: String!
messageIds: [String]
}
type ConversationConnection {
items: [Conversation]
nextToken: String
}
input CreateConversationInput {
id: String!
messageIds: [String]
}
input CreateMessageInput {
id: String!
text: String!
timestamp: String!
}
input CreateProfileInput {
id: Int!
name: String!
profileImage: String
isOnline: Boolean!
}
input DeleteConversationInput {
id: String!
}
input DeleteMessageInput {
id: String!
}
input DeleteProfileInput {
id: Int!
}
type Message {
id: String!
text: String!
timestamp: String!
}
type MessageConnection {
items: [Message]
nextToken: String
}
type Mutation {
createProfile(input: CreateProfileInput!): Profile
updateProfile(input: UpdateProfileInput!): Profile
deleteProfile(input: DeleteProfileInput!): Profile
createMessage(input: CreateMessageInput!): Message
updateMessage(input: UpdateMessageInput!): Message
deleteMessage(input: DeleteMessageInput!): Message
createConversation(input: CreateConversationInput!): Conversation
updateConversation(input: UpdateConversationInput!): Conversation
deleteConversation(input: DeleteConversationInput!): Conversation
updateConversationMessages(id: String!, messageIds: [String]): Conversation
}
type Profile {
id: Int!
name: String!
profileImage: String
isOnline: Boolean!
}
type ProfileConnection {
items: [Profile]
nextToken: String
}
type Query {
getProfile(id: Int!): Profile
listProfiles(first: Int, after: String): ProfileConnection
getMessage(id: String!): Message
listMessages(first: Int, after: String): MessageConnection
getConversation(id: String!): Conversation
listConversations(first: Int, after: String): ConversationConnection
}
type Subscription {
onCreateProfile(
id: Int,
name: String,
profileImage: String,
isOnline: Boolean
): Profile
@aws_subscribe(mutations: ["createProfile"])
onUpdateProfile(
id: Int,
name: String,
profileImage: String,
isOnline: Boolean
): Profile
@aws_subscribe(mutations: ["updateProfile"])
onDeleteProfile(
id: Int,
name: String,
profileImage: String,
isOnline: Boolean
): Profile
@aws_subscribe(mutations: ["deleteProfile"])
onCreateMessage(id: String, text: String, timestamp: String): Message
@aws_subscribe(mutations: ["createMessage"])
onUpdateMessage(id: String, text: String, timestamp: String): Message
@aws_subscribe(mutations: ["updateMessage"])
onDeleteMessage(id: String, text: String, timestamp: String): Message
@aws_subscribe(mutations: ["deleteMessage"])
onCreateConversation(id: String, messageIds: [String]): Conversation
@aws_subscribe(mutations: ["createConversation"])
onUpdateConversation(id: String, messageIds: [String]): Conversation
@aws_subscribe(mutations: ["updateConversation"])
onDeleteConversation(id: String, messageIds: [String]): Conversation
@aws_subscribe(mutations: ["deleteConversation"])
}
input UpdateConversationInput {
id: String!
messageIds: [String]
}
input UpdateMessageInput {
id: String!
text: String
timestamp: String
}
input UpdateProfileInput {
id: Int!
name: String
profileImage: String
isOnline: Boolean
}
Пока я могу создавать беседы на DynamoDB из клиента, но не обновлять их. Я пытался написать резолвер, но он не работает:
{
"version": "2017-02-28",
"operation": "UpdateItem",
"key": {
"id": { "S": "${util.context.arguments.id}" }
}
"update": {
"expression": "SET List = :List",
"expressionValues": {
#set( $List = $context.arguments.List )
":List": $util.dynamodb.toListJson($List)
}
}
}
Есть идеи, как добавить идентификатор последнего сообщения в массив messageIds разговора? Спасибо.
@vahdet это журнал, который я получаю: «UpdateItem требует предоставления выражения обновления».





В DynamoDB есть зарезервированные слова и среди них List. В этом случае необходимо использовать свойство expressionNames. См. раздел в документе:
Finally, DynamoDB has reserved words that cannot appear in the expression. [...], we can use name placeholders and define them in the expressionNames field as:
{
"version": "2017-02-28",
"operation": "UpdateItem",
"key": {
"id": { "S": "${context.arguments.id}" }
},
"update": {
"expression": "SET #List = :List",
"expressionNames": {
"#List" : "List"
},
"expressionValues": {
#set( $List = $context.arguments.List )
":List": $util.dynamodb.toListJson($List)
}
}
}
Для более сложного примера AppSync-DynamoDB (UpdateItem) вы можете посетить здесь.
Отличное объяснение @vahdet. У меня есть одно предложение с доступом к аргументу. Когда я смотрю в схему для обновления диалога updateConversation (input: UpdateConversationInput!): Conversation Аргумент вводится. Чтобы получить доступ к идентификатору, мы должны сделать что-то вроде $ context.arguments.input.id (более короткая версия: $ ctx.args.input.id)
Не могли бы вы поделиться включением CloudWatch Logs для схемы AppSync и сообщить об ошибке, которую вы там видите? На первый взгляд можно попробовать изменить объект
keyна"id" : { "S" : "${context.arguments.id}" }безutil.