如何在Hasura中建立多对多插入变异?

时间:2020-05-20 12:53:24

标签: graphql hasura

我正在Hasura中建立我的第一个多对多插入突变,并发现它很困难。 the docs中的语法和随附的说明很难遵循。

我只是想在Warning message: In sqrt(-9) : NaNs produced component之间添加连接。

这是我当前查询的状态。

module

这是我得到的错误。

mutation MyMutation {
  insert_component(objects: {component_module: {data: {module: {data: {id: "775c9e27-c974-4cfa-a01f-af50bd742726"}, on_conflict: {constraint: module_id_key, update_columns: id}}}}}) {
    affected_rows
    returning {
      id
      component_modules
    }
  }
}

预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的突变无效,因为您是手动插入ID,而当Hasura生成查询时,其父级中将没有ID。

进行嵌套插入时,最好的方法是让PostgreSQL为您生成ID。这样,您就可以在关系的任何一边进行插入。

在您的示例中,您实际上并不需要每个表中都有component_modules列。在进行多对多插入时,可以将每个表的ID用作外键。

例如:

component - id - created_at - updated_at - name module - id - created_at - updated_at - name component_module - component_id - module_id

突变应该是这样的:

mutation {
  insert_component(objects: {
    name:"component name",
    component_modules: {
      data: {
        module: {
          data: {
            name: "module name"
          }
        }
      }
    }
  }) {
    returning {
      id
      component_modules {
        component {
          name
        }
      }
    }
  }
}