0

I am using aws amplify datastore to connect my app to a dynamodb backend.

I have an Item type which has a property named tags, which is defined as Array of strings

type Item @model {
  id: ID!
  content: String!
  tags: [String]
}

The tags property is not required to be present. But when i save the Item without the tag property,

await DataStore.save(new Item({ content: 'abc' })

DataStore throws this error -

Error: Field tags should be of type string[], object received.null

So i tried saving an empty array when there were no tags

await DataStore.save(new Item({ content: 'abc', tags: [] })

But then dynamodb threw this error when trying to create the item

{
  localModel: {
   // item content and some extra fields
   "tags": Array [],
  },
  "message": "Supplied AttributeValue is empty, must contain exactly one of the supported datatypes (Service: DynamoDb, Status Code: 400, Request ID: 9AQ0STOFFLGPUGR1S16BTORT8VVV4KQNSO5AEMVJF66Q9ASUAAJG)"
}

How do i save a property which is an array of strings but optional?

Mukesh Soni
  • 6,646
  • 3
  • 30
  • 37

1 Answers1

2

Your tag property is defined as a nullable List of String elements tags: [String] which means that the list can be null but cannot be empty because of its type definiton. DataStore will warn you but DynamoDB resolver will force you.

If you are expecting to have null values or you want an empty list define your property like so

tags: [String!]

This means that the list itself can be null, but it can’t have any null members.

valid: null, [], ['a', 'b']

invalid: ['a', 'b', null]

tags: [String]!

This means that the list itself canot be null, but it can contain null members.

valid: [], ['a', 'b', null]

invalid: null

tags: [String!]!

This means that the list itself canot be null, and it cannot contain null members.

valid: [], ['a', 'b']

invalid: null, [], ['a', 'b', null]

Oscar Nevarez
  • 994
  • 12
  • 24
  • I will try out the combinations. But in your first example the valid and invlid items are the same. In the third example, `tags: [String!]!`, empty list, [], is both in valid and invalid list of elements. I am confused. – Mukesh Soni Jul 30 '20 at 04:05
  • I tried the first one - `tags: [String!]`. dynamodb still throws the same error when i send empty list ([]) :( – Mukesh Soni Jul 31 '20 at 04:58