【问题标题】:AppSync MutationAppSync 突变
【发布时间】:2018-12-24 04:01:02
【问题描述】:

我正在使用 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 数组的任何想法? 谢谢。

【问题讨论】:

  • 您能否分享为 AppSync 架构打开 CloudWatch Logs 并告诉您在那里看到的错误?乍一看,把key对象改成"id" : { "S" : "${context.arguments.id}" }不带util可以试试。
  • @vahdet 这是我收到的日志“UpdateItem 需要提供更新表达式。”

标签: amazon-dynamodb graphql aws-appsync


【解决方案1】:

DynamoDB 中有reserved wordsList 也在其中。在这种情况下,必须使用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

【讨论】:

  • 很好的解释@vahdet。我对参数访问有一个建议。当我查看更新对话的架构时, updateConversation(input: UpdateConversationInput!): Conversation 参数是输入。要访问 id,我们应该执行类似 $context.arguments.input.id(shorter version: $ctx.args.input.id)
猜你喜欢
  • 2018-11-05
  • 2019-04-13
  • 2018-11-21
  • 2018-12-02
  • 2019-12-27
  • 2020-08-24
  • 2021-05-13
  • 2019-03-05
  • 2020-05-30
相关资源
最近更新 更多