DaVinciLord
08/31/2018, 2:44 PMone-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:
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)
}
catalinmiron
08/31/2018, 3:06 PMctx.db.query.books({where:{author{id: "id"}}})
you're taking getting type Book
not type Author
.catalinmiron
08/31/2018, 3:06 PM.books
based on author id and .authors
based on book ids?catalinmiron
08/31/2018, 3:26 PMcatalinmiron
08/31/2018, 3:26 PMtype Author {
id: ID! @unique
name: String!
books: [Book!]! @relation(name: "AuthorBooks")
}
type Book {
id: ID! @unique
title: String!
author: Author! @relation(name: "AuthorBooks")
}
catalinmiron
08/31/2018, 3:29 PMthis 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)
DaVinciLord
08/31/2018, 5:09 PM{
book(id: "book_id") {
id
author { id }
}
}
catalinmiron
08/31/2018, 5:10 PMDaVinciLord
08/31/2018, 5:43 PMctx.db.query.author({ where: { id: book.authorId } })
in the resolver functioncatalinmiron
08/31/2018, 6:21 PMcatalinmiron
08/31/2018, 6:22 PMDaVinciLord
08/31/2018, 6:57 PM