向 Graphcool 电子邮件密码模板突变添加字段
Posted
技术标签:
【中文标题】向 Graphcool 电子邮件密码模板突变添加字段【英文标题】:Adding field to Graphcool email-password template mutation 【发布时间】:2018-11-17 15:33:44 【问题描述】:我使用电子邮件密码模板来设置我的 graphcool 服务器和用户身份验证。我想在注册新用户时为“名称”添加一个属性。我在 User 模型中添加了 name 并更新了 signup.graphql 和 signup.ts 代码,如下所示。
运行 createUser 突变时出现错误,提示“名称未定义”。我不确定问题是什么。非常感谢任何帮助!
signup.graphql
extend type Mutation
signupUser(email: String!, password: String!, name: String): SignupUserPayload
signup.ts
interface EventData
email: string
password: string
name: string
const SALT_ROUNDS = 10
export default async (event: FunctionEvent<EventData>) =>
console.log(event)
try
const graphcool = fromEvent(event)
const api = graphcool.api('simple/v1')
const email, password = event.data
if (!validator.isEmail(email))
return error: 'Not a valid email'
// check if user exists already
const userExists: boolean = await getUser(api, email)
.then(r => r.User !== null)
if (userExists)
return error: 'Email already in use'
// create password hash
const salt = bcrypt.genSaltSync(SALT_ROUNDS)
const hash = await bcrypt.hash(password, salt)
// create new user
const userId = await createGraphcoolUser(api, email, hash, name)
// generate node token for new User node
const token = await graphcool.generateNodeToken(userId, 'User')
return data: id: userId, token
catch (e)
console.log(e)
return error: 'An unexpected error occured during signup.'
async function getUser(api: GraphQLClient, email: string): Promise< User >
const query = `
query getUser($email: String!)
User(email: $email)
id
`
const variables =
email,
return api.request< User >(query, variables)
async function createGraphcoolUser(api: GraphQLClient, email: string, password: string, name: string): Promise<string>
const mutation = `
mutation createGraphcoolUser($email: String!, $password: String!, $name: String)
createUser(
email: $email,
password: $password,
name: $name
)
id
`
const variables =
email,
password: password,
name: name
return api.request< createUser: User >(mutation, variables)
.then(r => r.createUser.id)
【问题讨论】:
【参考方案1】:找到了答案。我在这里缺少这一行的名字..
const email, password = event.data
改成这个就解决了
const email, password, name = event.data
【讨论】:
以上是关于向 Graphcool 电子邮件密码模板突变添加字段的主要内容,如果未能解决你的问题,请参考以下文章
graphql/graphcool:如何编写一个连接一对一和一对多关系的突变