应用同步GraphQL:如何按嵌套值过滤列表 [英] Appsync & GraphQL: how to filter a list by nested value

查看:176
本文介绍了应用同步GraphQL:如何按嵌套值过滤列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由Amplify从基本架构生成的Appsync API.在Article模型上,category字段嵌套在metadata字段内.我想构建一个查询,该查询提供按类别过滤的文章列表.我不清楚如何过滤嵌套值...我已经看到

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转换模式

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
}

生成列表查询

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
  }
}
`;

过滤器查询失败

query listArticlesByCategory($category: String!) {
  listArticles(filter: {category: {eq: $category}}) { 
    items {
      title
      description
      text
      metadata {
        category
        creationDate
        lastModified
      }
    }
  }
}

Appsync控制台错误指出filter: {category: ... }中的category是未知字段.

The Appsync console error states that the category in filter: {category: ... } is an unknown field.

推荐答案

默认情况下,仅Amplify码源仅在顶级过滤器上运行.您可以扩展它以包括对嵌套在ArticleMetadata中的属性的过滤器.

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.

您将需要增强ModelArticleFilterInput类型以包括类别字段.假设商品表中的元数据字段由DynamoDB地图支持,则可以基于地图值进行过滤.当filter参数中有类别值时,您将需要修改listArticles解析器的请求映射模板" VTL以添加包含类似于metadata.category = :category之类的过滤器表达式.

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.

这篇关于应用同步GraphQL:如何按嵌套值过滤列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆