Jwt 如何从graphql中返回令牌

Jwt 如何从graphql中返回令牌,jwt,graphql,Jwt,Graphql,这是我的变异代码,我使用的用户类型有姓名、电子邮件、密码,我对注册用户和登录用户进行了两次变异。我已经搜索了所有关于graphql的文档,并阅读了所有与身份验证相关的博客,但没有找到从数据库返回令牌的答案 const mutation = new GraphQLObjectType({ name: "Mutation", fields: { addUser: { type: UserType, args: {

这是我的变异代码,我使用的用户类型有姓名、电子邮件、密码,我对注册用户和登录用户进行了两次变异。我已经搜索了所有关于graphql的文档,并阅读了所有与身份验证相关的博客,但没有找到从数据库返回令牌的答案

    const mutation = new GraphQLObjectType({
      name: "Mutation",
      fields: {
        addUser: {
          type: UserType,
          args: {
            name: { type: GraphQLString },
            email: { type: GraphQLString },
            password: { type: GraphQLString },
            avatar: { type: GraphQLString }
          },
          resolve(parentValue, args) {
            const avatar = gravatar.url(args.email);
            return bcrypt
              .hash(args.password, 10)
              .then(hash => {
               args.password = hash;
               const newUser = new User({
                  name: args.name,
                  email: args.email,
                  password: args.password,
                  avatar
                });
                return newUser
                  .save()
                  .then(user => user)
                  .catch(e => e);
              })
               .catch(e => e);
          }
        },
        login: {
          name: "Login",

          type: UserType,
          args: {
            email: { type: GraphQLString },
            password: { type: GraphQLString }
          },
          resolve(parentValue, args, context) {
            return User.findOne({ email: args.email })
              .then(user => {
                if (user) {
                  return bcrypt
                    .compare(args.password, user.password)
                    .then(isValid => {
                      if (!isValid) {
                        throw new Error({ message: "password Incrrect" });
                      } else {
                        const token = jwt.sign(
                          { name: user.name, id: user.id },
                          "mySecret"
                        );
                        return user;
                      }
                    })
                    .catch(e => e);
                } else {
                  throw new Error({ message: "email Incorrect" });
                }
              })
              .catch(e => e);
          }
        }
      }
    });
这是我的用户类型

    const UserType = new GraphQLObjectType({
      name: "User",
      fields: {
        id: { type: GraphQLString },
        name: { type: GraphQLString },
        email: { type: GraphQLString },
        password: { type: GraphQLString },
        avatar: { type: GraphQLString }
      }
    });

我建议您通过删除密码字段并添加令牌字段来更新您的
UserType
,如:

const UserType = new GraphQLObjectType({
      name: "User",
      fields: {
        id: { type: GraphQLString },
        name: { type: GraphQLString },
        email: { type: GraphQLString },
        avatar: { type: GraphQLString },
        token: { type: GraphQLString }
      }
 });
原因是,
UserType
是变异的返回类型,所以它是“public”,也许我们不应该向public发送密码(因为我们在服务器端进行身份验证),但JWT是public的,所以我们可以将其发送回去

在您的
登录中
将令牌添加到用户对象中,类似于:

   login: {
      name: "Login",
      type: UserType,
      args: { email: { type: GraphQLString }, password: { type: GraphQLString } },
      resolve(parentValue, args, context) {
        return User.findOne({ email: args.email })
          .then(user => {
        .........
                    const token = jwt.sign(
                      { name: user.name, id: user.id },
                      "mySecret"
                    );
                    user.token = token;
                    return user;
                  }
        ........
      }
    }