Mitrofanova Natali 2 jaren geleden
bovenliggende
commit
63161f09cb

HW 14 Async Await/index.html → HW14 Async Await/index.html


HW 14 Async Await/script.js → HW14 Async Await/script.js


HW 14 Async Await/style.css → HW14 Async Await/style.css


+ 23 - 0
HW15 REST и GraphQL/index.html

@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <title>GQL HW</title>
+        <meta charset='utf8' />
+        <style>
+            #mainContainer {
+                display: flex;
+            }
+            #aside {
+                width: 30%;
+            }
+            #aside > a{
+                display: block;
+            }
+        </style>
+    </head>
+    <body>
+        
+        <script src='script.js'></script>
+    </body>
+</body>
+</html>

+ 79 - 0
HW15 REST и GraphQL/script.js

@@ -0,0 +1,79 @@
+
+const getGQL = url =>
+    (query, variables) => fetch(url, {
+        method: 'POST',
+        headers: {                              //заголовок content-type
+            "Content-Type": "application/json",
+            ...(localStorage.authToken ? {"Authorization": "Bearer " + localStorage.authToken} : {})
+        },
+        body: JSON.stringify({query, variables})       //body с ключами query и variables
+    }).then(res => res.json())
+        .then(data => {
+            if (data.data){
+                console.log(data);
+                return data.data;
+            } 
+            else throw new Error(JSON.stringify(data.errors))
+        })
+
+const backendURL = 'http://shop-roles.asmer.fs.a-level.com.ua'
+
+const gql = getGQL(backendURL + '/graphql')
+
+
+function register(login, password) {
+    return gql(
+        `mutation reg($login:String, $password:String) {UserUpsert(user: {login:$login, password: $password}) {_id login} }`,
+        { login: login, password: password }
+    );
+}
+
+register("Natasha", "12345").then(res=>console.log(res));
+
+
+
+const  login = (login, password) => {
+    return gql(` query log ($login:String, $password:String){
+                                        login(login:$login, password: $password) }`,
+                                        {login:login, password:password})
+}
+login("Natasha","12345").then(res=>console.log(res))
+
+
+
+const rootCats= () => 
+ gql(`query {
+    CategoryFind(query: "[{\\"parent\\":null}]"){
+        _id name
+    }
+}`)
+rootCats().then(res=>console.log(res))
+
+
+const findCatById=(_id)=> gql(`query catById($q: String){
+    CategoryFindOne(query: $q){
+        _id name goods {
+            _id name price images {
+                url
+            }
+        }
+    }
+}`, {q: JSON.stringify([{_id}])} )
+
+findCatById("61715f99ef4e1b3e3b67703e").then(res=>console.log(res))
+
+
+const findGoodById=(_id)=> gql(`
+        query goodById($q: String){
+                GoodFindOne(query:$q){
+                            _id
+                            name
+                            price
+                            description
+                            categories{_id name}
+                        
+        }
+} `, {q: JSON.stringify([{_id}])})
+
+findGoodById("61782338ef4e1b3e3b677079").then(res=>console.log(res))
+