Appsync & GraphQL: how to filter a list by nested value

跟風遠走 提交于 2019-12-12 13:05:35

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/55132782/appsync-graphql-how-to-filter-a-list-by-nested-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!