0
votes

With thanks in advance as this is probably a 101 question - I can't find an answer anywhere.

I've set up what I think is a simple example of AppSync and DynamoDB.

In DynamoDB I have a categorys table, with items of the form

{
  slug: String!,
  nm: String,
  nmTrail: String,
  ...
}

So - no id field. slug is he primary partition key, not null and expected to be unique (is unique in the data I've got loaded so far).

I've set up a simplified AppSync schema in line with the above definition and a resolver...

{
  "version": "2017-02-28",
  "operation" : "GetItem",
  "key" : {
    "slug" : { "S" : "${context.arguments.slug}" }
  }
}

A query such as

query GoGetOne {
  getCategory(slug: "Wine") {
    nm
  }
}

Works fine - returning the nm value for the correct item in categorys - similarly I can add any of the other properties in categorys to return them (e.g. nmTrail) except slug.

If I add slug (the Primary Partition Key, a non-nullable String) to the result set then I get a DynamoDB:AmazonDynamoDBException of the provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException.

If I scan/query/filter the table in DynamoDB all is good.

Most of the AWS examples use an id: ID! field in the 'get one' examples and also ask for it as a returned item.

update1 in response to KDs request

My update mutation schema is:

type Mutation {
  putCategory(
    slug: String!,
    nm: String,
    nmTrail: String,
    mainCategorySlug: String,
    mainCategoryNm: String,
    parentCategorySlug: String,
    parentCategoryNm: String
  ): Category
}

No resolver associated with that and (obviously) therefore haven't used mutation to put anything yet - just trying to get batch uploaded data to begin with.

/update1

What am I missing?

1
Can I see your Insert Mutation Resolver Mapping ? - KoingDev

1 Answers

1
votes

I tried to reproduce your API as much as I could and it works for me.

Category DynamoDB table

enter image description here

Schema:

type Query {
    getCategory(slug: String!): Category
}
type Category {
    slug: String
    nm: String
    nmTrail: String
}

Resolver on Query.getCategory request template:

{
    "version": "2017-02-28",
    "operation": "GetItem",
    "key": {
        "slug": $util.dynamodb.toDynamoDBJson($ctx.args.slug),
    }
}

Resolver on Query.getCategory response template:

$util.toJson($ctx.result)

Query:

query GoGetOne {
  getCategory(slug: "Wine") {
    slug
    nm
  }
}

Results

{
  "data": {
    "getCategory": {
      "slug": "Wine",
      "nm": "Wine1-nm"
    }
  }
}