为什么AppSync在更新时引发错误?

时间:2018-09-12 03:03:15

标签: graphql aws-appsync

AWS AppSync变异在尝试执行更新操作时抛出错误。我已经正确创建了表格和

下面是GraphQL模式

type Mutation {
  updateStateMutation(input: UpdateData!): ReturnValue
}

input UpdateData {
  ID: String
  OPP: Int
  loc: [Float]
  CDC: String
  MND: String
  CSP: Int
}

type ReturnValue {
  ID: String
  CDC: String
  MND: String
  loc: [Float]
  CSP: Int
  OPP: Int
}

黄色是突变的解析器

{
    "version" : "2017-02-28",
    "operation" : "PutItem",
    "key" : {
        "ID":  $util.dynamodb.toDynamoDBJson($ctx.args.ID),
    },
    "attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args)
}

下面是我在GraphQL上执行的执行部分

mutation UpdateStateData {
  updateStateMutation(input: { 
    ID: "100000000-ofo"
    CDC: "3E5E65117E877076L"
    MND: "6EA8F0DAE8C3D09F"
    CSP: 2
  }){
    ID
    CDC
    MND
    CSP
    loc
    OPP
  }
}

执行后,出现以下错误。

{
  "data": {
    "updateStateMutation": null
  },
  "errors": [
    {
      ………
      "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: MRMBUAFKERB48R7JTH5TUV8I8NVV4KQNSO5AEMVJF66Q9ASUAAJG)"
    }
  ]
}

上述编码有什么问题。我已经正确给出了输入,但是仍然遇到错误。 DynamoDB中的密钥名称是ID,但是它仍然引发错误。

1 个答案:

答案 0 :(得分:1)

它引发错误,因为您尝试使用PutItem而不是UpdateItem操作,而忘记将id用作参数。您可能需要从ID输入中删除UpdateData,并将您的变异更改为类似这样的内容:

type Mutation {
  updateStateMutation(id: String!, input: UpdateData!): ReturnValue
}

您的解析器映射如下:

#set( $expression = "SET" )
#set( $expValues = {} )

#if( !$util.isNull(${context.arguments.input.OPP}) ) 
    #set( $expression = "${expression} OPP = :OPP" )
  $!{expValues.put(":OPP",  { "N" : ${context.arguments.input.OPP} })}
#end

#if( !$util.isNull(${context.arguments.input.loc}) ) 
    #if( ${expression} != "SET" ) 
        #set( $expression = "${expression}," )
    #end
    #set( $expression = "${expression} loc = :loc" )
  $!{expValues.put(":loc", $util.dynamodb.toDynamoDBJson($context.arguments.input.loc) )}
#end

#if( !$util.isNull(${context.arguments.input.CDC}) ) 
    #if( ${expression} != "SET" ) 
        #set( $expression = "${expression}," )
    #end
    #set( $expression = "${expression} CDC = :CDC" )
  $!{expValues.put(":CDC",  { "S" : ${context.arguments.input.CDC} })}
#end

#if( !$util.isNull(${context.arguments.input.MND}) ) 
    #if( ${expression} != "SET" ) 
        #set( $expression = "${expression}," )
    #end
    #set( $expression = "${expression} MND = :MND" )
  $!{expValues.put(":MND",  { "S" : ${context.arguments.input.MND} })}
#end

#if( !$util.isNull(${context.arguments.input.CSP}) ) 
    #if( ${expression} != "SET" ) 
        #set( $expression = "${expression}," )
    #end
    #set( $expression = "${expression} CSP = :CSP" )
  $!{expValues.put(":CSP",  { "N" : ${context.arguments.input.CSP} })}
#end

{
    "version" : "2017-02-28",
    "operation" : "UpdateItem",
    "key" : {
        "ID" : { "S" : "${context.arguments.id}" }
    },
    "update" : {
        "expression" : "${expression}",
        "expressionValues": $util.toJson($expValues)
    }
}

希望它有用! :)