I’m looking for some sort of way to initialize the ‘parent’ values which the fields later use in order to implement lazy loading. So I only load the whole object from DB only if needed.
// Schema
type Book {...}
UpdateBookReturn {
updatedBookId: ID!
updatedBook: Book
}
mutation {
updateBook(bookId: ID!, title: String, authorId: ID): UpdateBookReturn
}
// Resolver
Mutation = {
...
updateBook: (_parent, params) => {
void doBookUpdate(params)
return {
updatedBookId: params.bookId,
updatedBook: { __type: 'Book', id: parms.bookId }
}
},
}
Book {
// I want this to run first.
$someInit: (parent) => {
parent.rootValue = getBook(parent.id)
}
author: (parent) => parent.author
title: (parent) => parent.title
...
}
So if someone selects updatedBookId
it can return that right a way without the extra hit to the DB, but if they do select updatedBook
, it can load the book.
Note: This is over simplified example. The reason I’m looking for a init is because there are multiple mutations that will be using this. So I don’t want to use a async function inside the mutation return, which I’ll need to repeat.
Thanks.