i am new to graphql and i am having this issue and don't know how to solve it.
for example i have two tables: book and book_author;
book has book_id and name book_author has book_author_id, book_id, author_name
that two tables needs to be inserted values at a single request but the book_author table needs the book_id which from the book table it self with be generated.
can someone help me with this? help is much appreciated.
expected result is when calling post request or upon inserting.
for ex. the system generated book_id bk123, the book_id in the table book_author should be the same too.
It is possible - the important documentation is here: https://hasura.io/docs/latest/graphql/core/databases/postgres/mutations/multiple-mutations/#insert-an-object-and-a-nested-object-in-the-same-mutation
mutation {
insert_book_author(objects: {book: {data: {name: "New Book"}}, author_name: "Sarah"}) {
affected_rows
returning {
book_author_id
book {
book_id
name
}
}
}
}
Notice how an id is not specified in the nested book object. Hasura will automatically fill that in upon insert.
add a book
table
add a book_author
table, with foreign key to book
add an object relationship between book
and book_author
run a mutation with nested objects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With