Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

amazon web services - AWS amplify Graph QL filter by book title and author name

AWS amplify DynamoDB Graph QL filter by book title and author name

i want to search books by book title and author name but my schema allow me to search books by book title and author ID not author name how i can achieve this.

following is my graph ql schema

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are coming from the world of relational databases, this might seem like it should be trivial. In the world of DynamoDB it is more complex. You cannot create a @key that is linked to a @connection ( as far as I understand ). Some solutions to this problem:

1: Add Author's Name to Book

The author's name doesn't change typically, so you could do the below. Duplicating data is not frowned upon in DynamoDB/NoSQL world. This will give you a faster query as well.

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: Custom Resolvers

Custom resolvers, like @function ( Lambda functions ), or the more complex custom resolver templates can be used for multiple searches, and custom logic, though I would suggest option 1 first.

3: Exploring @searchable directive

See this for more info


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...