首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字段添加到Graphcool密码模板突变

将字段添加到Graphcool密码模板突变
EN

Stack Overflow用户
提问于 2018-06-07 16:19:19
回答 1查看 87关注 0票数 0

我使用电子邮件密码模板设置我的图形服务器与用户身份验证。当注册一个新用户时,我想为“name”添加一个属性。我向用户模型添加了名称,并更新了signup.graphql和signup.ts代码,如下所示。

当运行createUser突变时,我会得到一个错误,它说“没有定义名称”。我不知道问题出在哪里。任何帮助都是非常感谢的!

signup.graphql

代码语言:javascript
复制
extend type Mutation {
  signupUser(email: String!, password: String!, name: String): SignupUserPayload
}

signup.ts

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2018-06-07 16:25:38

找到答案了。我在这一行里漏掉了名字。

代码语言:javascript
复制
const { email, password } = event.data

变成这个解决了它

代码语言:javascript
复制
const { email, password, name } = event.data
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50745964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档