【问题标题】:Graphql - Is there a way how to insert two tables at the same time, but the second table is dependent on the return of from the first table?Graphql - 有没有办法同时插入两个表,但第二个表依赖于第一个表的返回?
【发布时间】:2019-12-30 16:55:56
【问题描述】:

我是 graphql 的新手,我遇到了这个问题,不知道如何解决。

例如,我有两个表:book 和 book_author;

book 有 book_id 和 name book_author 有 book_author_id、book_id、author_name

两个表需要在一个请求中插入值,但 book_author 表需要 book_id,它是从 book 表中生成的。

有人可以帮我解决这个问题吗?非常感谢您的帮助。

预期结果是在调用发布请求或插入时。

例如。系统生成book_idbk123,book_author表中的book_id也应该是一样的。

【问题讨论】:

    标签: javascript graphql mutation hasura


    【解决方案1】:

    这是可能的 - 重要的文档在这里: https://docs.hasura.io/1.0/graphql/manual/mutations/multiple-mutations.html#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
          }
        }
      }
    }
    

    注意在嵌套的 book 对象中没有指定 id。 Hasura 会在插入时自动填写。

    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

    【讨论】:

    • 谢谢@avimoondra 这真的有效!!!!!!我已经在我的代码上应用了它。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多