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

如何在AWS Amplify GraphQL中通过邮箱查询/更新用户(无ID)

解决方案

1. 通过邮箱查询用户

默认情况下,@model 生成的查询仅支持基于 id 的单条查询(getUsers),要实现邮箱查询,需先在Schema中给email字段添加全局二级索引:

修改后的Schema:

type Users @model @auth(rules: [{allow: public}]) {
  email: AWSEmail! @index(name: "byEmail", queryField: "getUserByEmail", sortKeyFields: [])
  access_token: String
  refresh_token: String
  company: String
  role: String
}
  • queryField 是自定义的查询字段名,这里设为getUserByEmail,Amplify会自动生成对应的查询方法
  • 更新Schema后,重新部署API(比如执行amplify push)

之后即可用邮箱直接查询用户:

query GetUserByEmail {
  getUserByEmail(email: "target@example.com") {
    id
    email
    role
    company
    access_token
    refresh_token
  }
}

2. 通过邮箱更新用户

GraphQL的更新Mutation(updateUsers)默认需要传入用户id,流程分两步:

  1. 用上面的getUserByEmail查询获取目标用户的id
  2. 传入id和要更新的字段执行更新

示例更新语句:

mutation UpdateUser {
  updateUsers(input: {
    id: "查到的用户ID"
    role: "admin"
    company: "新公司名称"
  }) {
    id
    email
    role
    company
  }
}

若想跳过先查id的步骤,可自定义Mutation并编写Resolver逻辑,但对新手而言,先查询再更新是最直接的方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 03:35:24