You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Firebase DataConnect中创建Organization时同步添加Admin成员?

嵌套创建Organization及管理员成员的Firebase DataConnect Mutation

完整Mutation代码

mutation CreateOrganizationWithOwnerMember(
  $slug: String!
  $name: String!
  $logo: String
  $ownerId: String!
) {
  insertOrganization(
    object: {
      slug: $slug
      name: $name
      logo: $logo
      owner: { connect: { id: $ownerId } }
      members: {
        create: {
          userId: $ownerId
          role: "ADMIN"
        }
      }
    }
  ) {
    id
    slug
    name
    members {
      userId
      role
    }
  }
}

关键逻辑说明

  • 关联组织Owner:通过owner: { connect: { id: $ownerId } }关联已存在的用户作为组织所有者,因为Organization的owner字段是必填的User!类型,必须关联已有用户(若需同时创建用户,可改为嵌套create,但需求中仅需关联现有用户)。
  • 嵌套创建管理员成员:在members字段下使用create语法,自动将新生成的Organization.id填充到OrganizationMember.organizationId,无需手动指定关联ID,同时设置role为ADMIN并关联同一个$ownerId。
  • 自动字段处理:id(uuidV4生成)、createdAt、updatedAt均为自动填充字段,无需在mutation中传入参数。

使用注意事项

  • 确保传入的$slug值唯一(表结构中slug标记了@unique),否则会触发唯一性约束错误。
  • 确认$ownerId对应的User记录已存在,否则connect操作会失败。

内容的提问来源于stack exchange,提问作者codercatdev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 02:37:33