在Graphql和节点

时间:2019-02-06 20:46:52

标签: node.js graphql

我正在尝试使用GraphQL和node创建一个登录功能。我已经注册成功,但是在查询登录功能时却说未定义密码。

AuthType

const AuthType = new GraphQLObjectType({
    name: 'Auth',
    fields: () => ({
        userId: {type: GraphQLString},
        username: {type: GraphQLString},
        email: {type: GraphQLString},
    })
});

这保存了我期望返回的数据。

const RootQuery = new GraphQLObectType({
  login: {
    type: AuthType,
    args: {
      password: {
        type: GraphQLString
      },
      email: {
        type: GraphQLString
      }
    },
    resolve(parent, args) {
      return User.findOne({
          email: args.email
        })
        .then(user => {
          const isEqual = new Promise(bcrypt.compare(password, args.password));
          if (!isEqual) {
            throw new Error('Password is incorrect!');
          }

        }).then(result => {
          return {
            userId: result.id,
            username: result.username
          };
        }).catch(err => {
          throw err
        });
    }
  }
});

这是检查数据的逻辑,谢谢。

schema.js

 const graphql = require('graphql');
    const bcrypt = require('bcryptjs');
    const jwt = require('jsonwebtoken');

    const {GraphQLObjectType, 
       GraphQLInt,
       GraphQLString,
       GraphQLSchema, 
       GraphQLID, 
       GraphQLList, 
       GraphQLNonNull } = graphql;

    const User = require('../models/user');
    const Event = require('../models/event');

用户类型定义了我们要存储的用户数据。

const UserType = new GraphQLObjectType({
    name: 'User',
    fields: () => ({
        id: {type: GraphQLID},
        firstname: {type: GraphQLString},
        lastname: {type: GraphQLString},
        username: {type: GraphQLString},
        email: {type: GraphQLString},
        password: {type: GraphQLString},
        location: {type: GraphQLString},
        about: {type: GraphQLString},
        gender: {type: GraphQLString},
        yob: {type: GraphQLString},      //Year of Birth;
        events: {
            type: new GraphQLList(EventType),
            resolve(parent, args){
            //  return _.filter(events, {userId: parent.id});
                return Event.find({creator: parent.id});
            }
        }


    })
});

登录功能仍然无法识别密码输入。

2 个答案:

答案 0 :(得分:0)

您没有在AuthType中定义密码字段,我想您应该这样做:

const AuthType = new GraphQLObjectType({
    name: 'Auth',
    fields: () => ({
        userId: {type: GraphQLString},
        username: {type: GraphQLString},
        email: {type: GraphQLString},
        password: {type: GraphQLString},
    })
});

此外,您在此行中有一个拼写错误:

const RootQuery = new GraphQLObectType({

应为GraphQLObjectType而不是GraphQLObectType

此外,在这一行:

const isEqual = new Promise(bcrypt.compare(password, args.password));

可能由于在代码中未定义password导致出现错误。您可能想做user.password吗?

答案 1 :(得分:0)

我使用{email,password}而不是在resolve函数中使用args。

        const RootQuery = new GraphQLObectType({
              login: {
                    type: AuthType,
                    args: {
                           password: {
                                 type: GraphQLString
                            },
                          email: {
                                 type: GraphQLString
                                      }
                         },
              resolve(parent, {email, password }) {
                   return User.findOne({
                        email: email
                   })
                    .then(user => {
                     const isEqual = bcrypt. compare(
                            password, user.password));
                      if (!isEqual) {
                           throw new Error('Password is incorrect!');
                       }

                    }).then(result => {
            return {
              userId: result.id,
             username: result.username
            };
          }).catch(err => {
             throw err
          });
       }