AWS amplify Graph QL 按书名和作者姓名过滤
Posted
技术标签:
【中文标题】AWS amplify Graph QL 按书名和作者姓名过滤【英文标题】:AWS amplify Graph QL filter by book title and author name 【发布时间】:2021-04-06 19:16:58 【问题描述】:AWS amplify DynamoDB Graph QL 按书名和作者姓名过滤
我想按书名和作者姓名搜索书籍,但我的架构允许我按书名和作者 ID 而不是作者姓名搜索书籍我如何实现这一点。
以下是我的图形 ql 架构
type Author
@model(subscriptions: null)
@auth(
rules: [
# allow admins to create, update and delete books
allow: groups, groups: ["owner"]
# allow all authenticated users to view books
allow: private, operations: [read]
]
)
@key(name: "authorByCreatedAt", fields: ["isDeleted", "createdAt"], queryField: "authorByCreatedAt")
id: ID!
name: String!
description: String!
status : Boolean!
createdAt: String!
image: String!
isDeleted: Int!
books: [Book] @connection(keyName: "byAuthor", fields: ["id"])
type Book
@model(subscriptions: null)
@auth(
rules: [
# allow admins to create, update and delete books
allow: groups, groups: ["owner"]
# allow all authenticated users to view books
allow: private, operations: [read]
]
)
@key(name: "bookByCreatedAt", fields: ["isDeleted", "createdAt"], queryField: "bookByCreatedAt")
@key(name: "byAuthor", fields: ["authorId"])
id: ID!
title: String!
description: String!
image: String!
age: Int!
region: String!
isbn: String
narrator: String
status : Boolean!
createdAt: String!
isDeleted: Int!
book: String!
bookType: BookType!
authorId: ID!
authors: Author @connection(fields: ["authorId"])
enum BookType
AUDIO
EPUB
【问题讨论】:
【参考方案1】:如果您来自关系数据库领域,这似乎应该是微不足道的。在 DynamoDB 的世界中,它更加复杂。您不能创建链接到@connection 的@key(据我所知)。这个问题的一些解决方案:
1:在书中添加作者姓名
作者的姓名通常不会更改,因此您可以执行以下操作。在 DynamoDB/NoSQL 世界中,复制数据并不令人讨厌。这也会为您提供更快的查询。
type Book
@model(subscriptions: null)
@key(name: "BooksByAuthorName", fields: ["authorName"], queryField: "getBooksByAuthorName")
id: ID!
title: String!
description: String!
image: String!
age: Int!
region: String!
isbn: String
narrator: String
status : Boolean!
createdAt: String!
isDeleted: Int!
book: String!
bookType: BookType!
authorId: ID!
authorName: String
authors: Author @connection(fields: ["authorId"])
2:自定义解析器
自定义解析器,如 @function(Lambda 函数),或更复杂的 custom resolver 模板可用于多个搜索和自定义逻辑,但我建议先使用选项 1。
3:探索@searchable指令
See this for more info
【讨论】:
以上是关于AWS amplify Graph QL 按书名和作者姓名过滤的主要内容,如果未能解决你的问题,请参考以下文章
如何按日期(createdAt)对aws-amplify中列表查询中的字段进行排序?
AWS Amplify with GraphQL - 按不同类型的用户定义身份验证规则
在 AWS Amplify GraphQL 中对结果进行排序而不进行过滤
aws-amplify-react 和 @aws-amplify/ui-react 有啥区别?