Ivan Asmer 5 years ago
parent
commit
d9e5db4f74
2 changed files with 16 additions and 3 deletions
  1. 8 3
      index.js
  2. 8 0
      models.js

+ 8 - 3
index.js

@@ -72,11 +72,13 @@ const anonResolvers = ['login', 'createUser'];
             count: Float,
             good: Good,
             order: Order
+            total: Float
         }
 
         type Order {
             _id: ID
             orderGoods: [OrderGood]
+            total: Float
         }
 
 
@@ -300,15 +302,17 @@ const anonResolvers = ['login', 'createUser'];
             }
         }
         const operationMatch = gql.query.match(/\{\s*([a-zA-Z]+)\s*/)
-        const operationName  = operationMatch[1]
-        const authorization = req.headers.authorization 
-        if (operationName === null || anonResolvers.includes(operationName)){
+        const operationName  = gql.operationName || operationMatch[1]
+        console.log('before oper', operationName, operationMatch)
+        if ((!operationName) || anonResolvers.includes(operationName)){
             return {
                 schema: schema,
                 rootValue: rootResolvers,
                 graphiql: true, 
             }
         }
+        const authorization = req.headers.authorization 
+        console.log(authorization)
         
         if (authorization && authorization.startsWith('Bearer ')){
             console.log('token provided')
@@ -328,6 +332,7 @@ const anonResolvers = ['login', 'createUser'];
                 }
             }
         }
+        console.log('bad end')
     }))
 
     app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));

+ 8 - 0
models.js

@@ -58,6 +58,10 @@ module.exports = async (dbName='shop') => {
                 this.orderGoods = this.orderGoods instanceof Array ? this.orderGoods : (this.orderGoods ? [this.orderGoods] : []) 
             }
 
+            get total(){
+                return (async() => (await Promise.all(this.orderGoods)).reduce((a,b) => a.total + b.total))()
+            }
+
             static get relations(){
                 return {
                     user: "orders",
@@ -72,6 +76,10 @@ module.exports = async (dbName='shop') => {
                 super(...params)
             }
 
+            get total(){
+                return this.price*this.count
+            }
+
             static get relations(){
                 return {
                     good: "orderGoods",