I have an Appsync API generated by Amplify from a basic schema. On the Article
model, a category
field is nested within a metadata
field. I want to build a Query that provides a list of Articles filtered by category. It is not clear to me how to filter on a nested value... I have seen similar questions but the analogous answer has not worked.
AWS GraphQL Transform Schema
type Article @model {
id: ID!
title: String!
description: String!
text: String!
metadata: ArticleMetadata!
}
type ArticleMetadata {
category: Category!
lastModified: String!
creationDate: String!
}
enum Category {
javascript
java
ruby
python
haskell
}
Generated List Query
export const listArticles = `query ListArticles(
$filter: ModelArticleFilterInput
$limit: Int
$nextToken: String
) {
listArticles(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
title
description
text
metadata {
category
lastModified
creationDate
}
}
nextToken
}
}
`;
Failing filter query
query listArticlesByCategory($category: String!) {
listArticles(filter: {category: {eq: $category}}) {
items {
title
description
text
metadata {
category
creationDate
lastModified
}
}
}
}
The Appsync console error states that the category
in filter: {category: ... }
is an unknown field.
By default the Amplify codegen only will operate on top-level filters. You can extend this to include filters for the attributes nested in ArticleMetadata.
You will need to augment the ModelArticleFilterInput
type to include the category field. Assuming that the metadata field in the article table is backed by a DynamoDB map, you can filter based on the map value. You will need to modify the listArticles resolver's Request Mapping Template VTL to add the filter expression that contains something like metadata.category = :category
when there is a value for cateogry in the filter argument.