Hey guys, I need some help as I don't have idea on...
# orm-help
d
Hey guys, I need some help as I don't have idea on how to do that. I have a
one-to-many
relation between two types. I can get the elements from this direction
one -> many
but I cannot get the element from the other direction. Example:
Copy code
type Author {
  id: ID! @unique
  books: [Book!]! // can get by invoking ctx.db.query.books({where:{author{id: "id"}}})
}

type Book {
  id: ID! @unique
  author: Author! <- // I don't know how to get this one from only the fields the book have, (console.log only show the id in this case)
}
c
Actually with
ctx.db.query.books({where:{author{id: "id"}}})
you're taking getting
type Book
not
type Author
.
What exactly would you like to take?
.books
based on author id and
.authors
based on book ids?
You have to create a relation in order to achieve what you'd like
Copy code
type Author {
  id: ID! @unique
  name: String!
  books: [Book!]! @relation(name: "AuthorBooks")
}

type Book {
  id: ID! @unique
  title: String!
  author: Author! @relation(name: "AuthorBooks")
}
Copy code
this will get all books by author id
ctx.db.query.books({
  where:{
    author:{
      id: "author_id"
    }
  }
}, info)


This will get all books by author
ctx.db.query.authors({
  where:{
    id: "author_id"
  }
}, info)
d
I would like to take the author of a book like in this graphql query:
Copy code
{
  book(id: "book_id") {
    id
    author { id }
  }
}
c
Everything I mentioned above should work for you. Try the relation
d
Something along the like of
ctx.db.query.author({ where: { id: book.authorId } })
in the resolver function
c
No, ctx.db.query.book({where: {author: id: “userid”}}}, info).
Pllease try following the documention and check the query section. I think that after reading it you should be able to build what you’d like to build.
d
Okay, I'll recheck the docs. Thanks for your help :)