Browse Source

+count to give ability get count via graphql when result type isnt array but Int

Ivan Asmer 4 years ago
parent
commit
b4e9ccf133
2 changed files with 33 additions and 7 deletions
  1. 19 6
      anon.js
  2. 14 1
      expand.js

+ 19 - 6
anon.js

@@ -1,8 +1,22 @@
 const { buildSchema } = require('graphql');
-const jwt         = require('jsonwebtoken')
+const jwt             = require('jsonwebtoken')
+const { createHash }  = require('crypto')
+
+const salt            = "IF384"
+
 module.exports = ({Savable, secret}) => {
     class User extends Savable {
+        async getACL(){
+            return [this._id.toString(), "user"]
+        }
 
+        set password(pwd){
+            this._password = User.getHash(pwd)
+        }
+
+        static getHash(pwd){
+            return createHash('sha256').update(pwd).update(salt).digest('hex')
+        }
     }
     Savable.addClass(User)
 
@@ -21,18 +35,17 @@ module.exports = ({Savable, secret}) => {
             return await user.save()
         },
 
-        login: async function({login, password}){
-            console.log(Savable.classes)
-            const user =  await Savable.m.User.findOne({login, password})
+        async login({login, password}){
+            const user =  await Savable.m.User.findOne({login, _password: User.getHash(password)})
             if (!user)
                 return null;
 
-            const token = jwt.sign({ sub: {id: user._id, login}}, secret); //подписывам токен нашим ключем
+            const token = jwt.sign({ sub: {id: user._id, login, acl: await user.getACL()}}, secret); //подписывам токен нашим ключем
             return token
         },
 
         changePassword:async function ({login, password, newPassword}){
-            const user =  await Savable.m.User.findOne({login, password})
+            const user =  await Savable.m.User.findOne({login, _password: User.getHash(password)})
             if (!user) return null;
             user.password = newPassword;
             return await user.save()

+ 14 - 1
expand.js

@@ -1,4 +1,4 @@
-const { buildSchema, GraphQLObjectType, GraphQLString, GraphQLList, GraphQLSchema } = require('graphql');
+const { buildSchema, GraphQLObjectType, GraphQLString, GraphQLInt GraphQLList, GraphQLSchema } = require('graphql');
 const ObjectID    = require("mongodb").ObjectID;
 function mmExpandSchema(gqlSchema){
     const types    = {}
@@ -76,6 +76,19 @@ function mmExpandSchema(gqlSchema){
                     }
                     queryFields[`${outputTypeName}Find`] = find
 
+                    const count = {
+                        type: GraphQLInt,
+                        args: {query: {type: GraphQLString}},
+                        async resolve(root, args, context, info){
+                            //console.log(root, args, context, info)
+                            args = JSON.parse(args.query)
+                            args[1] = args[1] || {}
+                            args[1].count = []
+                            return find.resolve(root, args, context, info)
+                        }
+                    }
+                    queryFields[`${outputTypeName}Count`] = count
+
                     const findOne = {
                         type: _typeMap[outputTypeName],
                         args: {query: {type: GraphQLString}},