AppSync突变

时间:2018-07-16 14:53:19

标签: amazon-dynamodb graphql aws-appsync

我正在使用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)
    }
 }
}

关于如何将最后一条消息的ID附加到对话messageIds数组的任何想法? 谢谢。

1 个答案:

答案 0 :(得分:2)

DynamoDB中有reserved words,其中有List。在这种情况下,必须使用expressionNames属性。参见section in the doc

  

最后,DynamoDB具有保留词,这些保留词不能出现在表达式中。 [...],我们可以使用名称占位符,并在expressionNames字段中将其定义为:

{
  "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)示例,您可以访问here