Ivar 2 vuotta sitten
vanhempi
commit
30a989781a
5 muutettua tiedostoa jossa 125 lisäystä ja 6 poistoa
  1. 88 6
      js/15_graph-ql/index.js
  2. 0 0
      js/16_redux-thunk/index.html
  3. 0 0
      js/16_redux-thunk/index.js
  4. 13 0
      js/17/index.html
  5. 24 0
      js/17/index.js

+ 88 - 6
js/15_graph-ql/index.js

@@ -33,9 +33,91 @@
 
 
 
-localStorage.authToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2MWE0ZGIyOWM3NTBjMTJiYTZiYTQwMjIiLCJsb2dpbiI6ImVxd2VxZXdldyIsImFjbCI6WyI2MWE0ZGIyOWM3NTBjMTJiYTZiYTQwMjIiLCJ1c2VyIl19LCJpYXQiOjE2MzgxOTk4NDl9.wzpQZ3KjywbNSfCt0Z_dua2AIrADL-ID-4Rs2cVnq0Q" 
-const originalFetch = fetch;
-fetch = (url, params={headers:{}}) => { 
-    params.headers.Authorization = "Bearer " + localStorage.authToken
-    return originalFetch(url, params)
-}
+
+
+
+
+
+
+   // добавить заголовок Authorization если в localStorage есть authToken (не забудьте слово Bearer  и пробел после него. 
+   // Таким образом все запросы будут уходить не анонимно если в localStorage есть токен.
+
+   const originalFetch = fetch;
+   fetch = (url, params={headers:{}}) => { 
+      if (localStorage.authToken !== undefined) {
+         params.headers.Authorization = "Bearer " + localStorage.authToken
+      }
+      return originalFetch(url, params)
+   }
+
+
+   const getGQL = (url) => (
+      (query, variables) => fetch(url, {
+         method: 'POST',
+         headers: {
+               "Content-Type": "application/json"
+         },
+         body: JSON.stringify({query, variables})
+         
+      }).then(res => res.json()).then((data) => {
+
+         if ('errors' in data) {
+            Promise.reject(new Error('ОШИБКА'))
+            // throw new Error('GFSFSDFS')
+         } else {
+            return data.data
+         }
+
+      })
+   );
+
+
+   (async function() {
+      const gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
+
+      console.log((await gql(`query cats($q:String) {
+         CategoryFind(query:$q){
+            name goods{
+               name images{
+               url
+               }
+            }
+         }
+      }`, {q: "[{}]"})))
+
+
+
+      console.log((await gql(`
+         query NameForMe1($login:String, $password:String){
+            login(login:$login, password:$password)
+         }
+      `, {login: 'tst', password: '123'})))
+
+
+
+      console.log(await gql(`query userFind {
+         UserFind(query: "[{}]") {
+             login
+             nick
+         }
+     }`, {q: "[{}]"}))
+
+
+
+
+      // написать функции по типу:    
+      // для логина, регистрации, а так же для остальных страниц будущего сайта, которые вам пришли на ум.
+      function catById(_id){
+         return gql(`query catById($query:String) {
+                     CategoryFindOne(query:$query) {
+                        name goods{
+                           _id name
+                        }
+                     }
+         }`, {query: JSON.stringify([{_id}])})
+      }
+     console.log(await catById("5dc45acf5df9d670df48cc48"))
+     
+
+   })()
+

js/16/index.html → js/16_redux-thunk/index.html


js/16/index.js → js/16_redux-thunk/index.js


+ 13 - 0
js/17/index.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<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>
+<body>
+   
+   <script src="./index.js"></script>
+</body>
+</html>

+ 24 - 0
js/17/index.js

@@ -0,0 +1,24 @@
+htmlTree()
+function htmlTree() {
+
+}
+
+htmlTree()
+function htmlTree() {
+
+}
+
+htmlTree()
+function htmlTree() {
+
+}
+
+htmlTree()
+function htmlTree() {
+
+}
+
+htmlTree()
+function htmlTree() {
+
+}