【问题标题】:AWS AppSync - Create Mutation returns NullAWS AppSync - 创建突变返回 Null
【发布时间】:2019-03-05 09:20:30
【问题描述】:

我有一个简单的 AppSync API,其类型为 City 和 Venue,示例是映射小型零售商店。我正在尝试通过 AppSync 中的“查询”窗格创建一个新城市,我还想提供一个缩短的 ID - 而不是长的 autoId,这样我就可以对缩短的 ID 和前端应用程序运行查询从 URL 中的参数中使用它。

我正在尝试使用以下代码在 DynamoDB 中创建一个新项目:

架构

type City {
  id: String!
  name: String!
}

type Mutation {
  ...
  createCity(input: CreateCityInput!): City
  ...
}

input CreateCityInput {
  id: String!
  name: String!
}

Mutation.createCity

// Request mapping template

{
  "version" : "2017-02-28",
  "operation" : "PutItem",
  "key" : {
    ## If object "id" should come from GraphQL arguments, change to $util.dynamodb.toDynamoDBJson($ctx.args.id)
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.id)
  },
  "attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args)
}

// Response mapping template
$util.toJson($context.result)

这是我在查询页面中运行的突变

mutation CreateCity {
  createCity(input: {
    id: "11538062"
    name: "Manchester"
  }) {
    id
    name
  }
}

这是从 AppSync 和/或 Dynamo 返回的错误

{
  "data": {
    "createCity": null
  },
  "errors": [
    {
      "path": [
        "createCity"
      ],
      "data": null,
      "errorType": "DynamoDB:AmazonDynamoDBException",
      "errorInfo": null,
      "locations": [
        {
          "line": 32,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "One or more parameter values were invalid: Type mismatch for key id expected: S actual: NULL (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 7PU4D2...)"
    }
  ]
}

任何帮助将不胜感激,我不明白为什么我无法使用提供的字符串作为 ID 执行简单的创建,并将其标识为 NULL...

【问题讨论】:

    标签: amazon-web-services graphql aws-appsync


    【解决方案1】:
    ## Request Mapping
    
    {
      "version" : "2017-02-28",
      "operation" : "PutItem",
      "key" : {
        "id" : $util.dynamodb.toDynamoDBJson($ctx.args.input.id)
      },
      "attributeValues" : {
        "name" : $util.dynamodb.toDynamoDBJson($ctx.args.input.name)
      }
    }
    

    【讨论】:

    • 感谢@KoingDev - 我将attributeValues 更改为“attributeValues”:$util.dynamodb.toMapValuesJson($ctx.args.input)
    猜你喜欢
    • 2019-01-01
    • 2018-12-21
    • 2019-03-07
    • 2020-12-28
    • 2019-04-13
    • 2019-12-27
    • 2019-10-31
    • 2018-12-02
    • 2018-12-24
    相关资源
    最近更新 更多