【问题标题】:AWS amplify Graph QL filter by book title and author nameAWS amplify Graph QL 按书名和作者姓名过滤
【发布时间】: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
}

【问题讨论】:

    标签: amazon-web-services graphql amazon-dynamodb aws-amplify aws-appsync


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多