Browse Source

gqlHW done

Vadym Hlushko 3 years ago
parent
commit
061a1f84c2
3 changed files with 146 additions and 55 deletions
  1. 1 0
      hw16/index.html
  2. 145 0
      gqlHw/main.js
  3. 0 55
      hw16/main.js

+ 1 - 0
hw16/index.html

@@ -2,6 +2,7 @@
 <html lang="en">
 <head>
     <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Document</title>
 </head>

+ 145 - 0
gqlHw/main.js

@@ -0,0 +1,145 @@
+const getGQL = url => 
+    (query, variables) =>
+        fetch(url, {
+            method: 'POST',
+            headers: {
+                "content-type": "application/json",
+                ...(localStorage.authToken ? {Authorization: "Bearer " + localStorage.authToken} : {})
+            },
+            body: JSON.stringify({query, variables})
+        }).then(res => res.json())
+
+    let gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql")
+
+    
+
+      let reg = async(login,password) => {
+    let query = `mutation reg($l:String , $p:String) {
+        UserUpsert(user:{
+          login:$l ,
+          password:$p
+        }){
+          _id
+        }
+      }`
+
+      let qVariables = {
+        "l":  login,
+        "p": password
+      }
+      let result = await gql(query,qVariables)
+      return result
+    }
+
+    reg ('v3rtex' , 'senior')
+
+    let log = async(login , password) => {
+        let query = ` query log($l:String , $p:String){
+            login(login:$l, password:$p)
+          }`
+    let qVariables = {
+        "l":  login,
+        "p": password 
+    }
+    let token = await gql(query,qVariables)
+    console.log(token)
+    localStorage.authToken = token.data.login
+    }
+
+    log('v3rtex' , 'senior')
+
+    let newOrder = async(obj) => {
+        let option = Object.entries(obj)
+        let orderGoods = []
+        for (let key of option) {
+            let iteration = {
+                "count": key[1],
+                "good":{"_id":key[0]}
+            }
+            orderGoods.push(iteration)
+        }
+        let query = `mutation newOrder($order:OrderInput) {
+            OrderUpsert(order:$order){
+              _id
+            }
+          }`
+
+          let qVariables = {
+            "order": {
+                "orderGoods": orderGoods}
+          }
+          let result = await gql(query,qVariables)
+          console.log(result)
+          return result
+    }
+
+    newOrder({ "5dc45d0b5df9d670df48cc4b":10})
+
+    let allOrders = async() => {
+        let res = await gql (`query orders {
+            OrderFind(query:"[{}]"){
+              _id total 
+          orderGoods{
+            good{
+              name
+            }
+            price
+            count
+            total
+          }
+            }
+          }`)
+          console.log(res)
+          return res
+    }
+
+    allOrders()
+
+    let orderById = async(id) => {
+        let query = `query orderID {
+            OrderGoodFindOne(query:"[{}]"){
+              _id
+              good{
+                _id
+                name
+              }
+            }
+          }`
+
+          let qVariables = {id}
+
+          let res = await gql(query,qVariables)
+          console.log(res)
+          return res
+    }
+
+   orderById("611f904bc456ed5e4d9d682b")
+    
+    async function goodFind() {
+        await gql(`query {
+            GoodFind(query: "[{}]"){
+                _id
+                name
+                price
+                images{
+                    url
+                }
+            }
+        }`
+        )}
+        
+        goodFind()
+        
+        async function categoryFind() {
+            await gql(`query {
+                CategoryFind(query: "[{}]"){
+                    _id
+                    name
+                    goods {
+                        _id name 
+                    }
+                }
+            }`).then(result => console.log(result))
+    }
+
+    categoryFind()

+ 0 - 55
hw16/main.js

@@ -1,55 +0,0 @@
-    const getGQL = url => 
-    (query, variables) =>
-        fetch(url, {
-            method: 'POST',
-            headers: {
-                "content-type": "application/json"
-            },
-            body: JSON.stringify({query, variables})
-        }).then(res => res.json())
-
-    let gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
-
-   async function goodFind() {
-       await gql(`query {
-        GoodFind(query: "[{}]"){
-            _id
-            name
-            price
-            images{
-                url
-            }
-        }
-    }`
-    ).then(res=>console.log(res))}
-
-    goodFind()
-
-    async function categoryFind() {
-        await gql(`query {
-            CategoryFind(query: "[{}]"){
-                _id
-                name
-                goods {
-                    _id name 
-                }
-            }
-        }`).then(result => console.log(result))
-    }
-
-    categoryFind()
-
-//eto mne nel'zya
-// async function orderGood() {
-//     await gql(`query {
-//                     OrderGoodFind(query: "[{}]"){
-//                         _id
-//                         price
-//                         count
-//                         good{
-//                             _id
-//                         }
-//                     }
-//                 }`).then(result => console.log(result))
-//             }
-// orderGood()