AWS 按书名和作者姓名放大 Graph QL 过滤器

AWS amplify Graph QL filter by book title and author name

AWS 按书名和作者姓名放大 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
}

如果您来自关系数据库领域,这似乎应该是微不足道的。在 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 functions ), or the more complex custom resolver 模板可用于多次搜索和自定义逻辑,但我建议首先使用选项 1。

3:探索@searchable 指令

See this for more info