简体   繁体   English

从Promise返回数据到GraphQL

[英]Return data to GraphQL from Promise

I'm having some trouble returning data to a GraphQL mutation. 我在将数据返回到GraphQL突变时遇到麻烦。 In the mutation, you provide an email and password to sign up. 在变异中,您提供电子邮件和密码进行注册。 From there, GraphQL should return a JSON web token containing the usersId. 从那里,GraphQL应该返回一个包含usersId的JSON Web令牌。

Even though the password is being hashed and the email and password is being saved to the database and the JWT is made with the users id as a payload, it responds with this 即使密码被散列并且电子邮件和密码被保存到数据库中,并且JWT是使用用户ID作为有效负载创建的,它也会以此响应

{
  "data": {
    "signUp": {
      "token": null,
      "email": null
    }
  }
}

Here is the GraphQL Query: 这是GraphQL查询:

mutation {
  signUp(email: "johndoe@example.com", password: "password") {
    token //Should return a JWT
    email // Should return the users email address
  }
}

Here is the mutation: (When the mutation is run, it logs the JWT to the console but doesnt return it to GraphQL) 这是突变:(运行突变时,它将JWT记录到控制台,但不将其返回给GraphQL)

const mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
    signUp: {
      type: UserType,
      args: {
        email: { type:  new GraphQLNonNull(GraphQLString) },
        password: { type: new GraphQLNonNull(GraphQLString) }
      },
      resolve (parentValue, args) {
        return signUp(args) // Calls a function in another file with the args
          .then((result) => {
            console.log(result) // Logs the JWT to the console.
            return result
          })
      }
    }
  }
})

Here is the userType: 这是userType:

const UserType = new GraphQLObjectType({
  name: 'UserType',
  fields: {
    id: { type: GraphQLID },
    email: { type: GraphQLString },
    token: { type: GraphQLString }
  }
})

Here us the signUp function: 这是我们的signUp函数:

function signUp ({ email, password }) {
  return new Promise((resolve, reject) => {
    bcrypt.hash(password, 10, function(err, password) {
      const userKey = datastore.key('User')
      const entity = {
        key: userKey,
        data: {
          email,
          password
        }
      }

      datastore.insert(entity)
        .then(() => {
          let userId = userKey.path[1]
          jwt.sign({userId}, 'secret', function (err, token) {
            resolve(token)
          })
        })
    })
  })
}

Following your comment : 您发表评论后

As your signUp mutation is of UserType , you should not resolve it with an object { token: ... } but with an User object. 由于您的signUp突变属于UserType ,因此您不应使用对象{ token: ... }解决它,而应使用User对象来解决它。 That will allow you to query others fields on the User when executing the mutation. 这样您就可以在执行突变时查询用户的其他字段。

Following your example, that could be: 按照您的示例,可能是:

function signUp ({ email, password }) {
  return new Promise((resolve, reject) => {

    bcrypt.hash(password, 10, function(err, password) {
      if (err) return reject(err);

      const userKey = datastore.key('User')
      const userId = userKey.path[1];

      jwt.sign({userId}, 'secret', function (err, token) {
        if (err) return reject(err);

        const entity = {
          key: userKey,
          data: {
            email,
            password,
            token,
          },
        };

        datastore.insert(entity)
          .then(inserted => resolve(inserted));
      });
    });

  });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM