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.
AWS AppSync allows your applications to access exactly the data they need. Create a flexible API to securely access, manipulate, and combine data from multiple sources. Pay only for requests to your API and for real-time messages delivered to connected clients.
AWS AppSync provides a robust, scalable GraphQL interface for application developers to combine data from multiple sources, including Amazon DynamoDB, AWS Lambda, and HTTP APIs.
AppSync is a serverless GraphQL service. In simple terms, it's just an API gateway based on the GraphQL specification you can pay on-demand. GraphQL offers a friendly query language on top of HTTP, making it possible to fetch multiple different resources in one request, lowering network latency.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With