Desmond
Desmond

Reputation: 1368

Why does AppSync throwing error on Update?

AWS AppSync mutation is throwing error when trying to perform update operation. I have correctly created the table and my

Bellow is the GraphQL Schema

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
}

Bellow is the resolver for the mutation

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

Bellow is the execution part that I perform on the GraphQL

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

When executed I am getting the error shown below.

{
  "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)"
    }
  ]
}

What is wrong with the above coding. I have correctly given the input but still I am facing the error. The Key name in DynamoDB is ID but, it is still throwing the error.

Upvotes: 0

Views: 1531

Answers (1)

KoingDev
KoingDev

Reputation: 620

It throws error because you are trying to use PutItem instead of UpdateItem operation and forget to put id as argument. You might need to remove ID from your UpdateData input and change your mutation to something like this:

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

Your resolver mapping would look like:

#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)
    }
}

Hope it is useful! :)

Upvotes: 2

Related Questions