Browse Source

<hw_graphql> done

Mark 1 year ago
parent
commit
a2522af2ed
2 changed files with 93 additions and 0 deletions
  1. 15 0
      15/index.html
  2. 78 0
      15/main.js

+ 15 - 0
15/index.html

@@ -0,0 +1,15 @@
+<!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="main.js"></script>
+</body>
+
+</html>

+ 78 - 0
15/main.js

@@ -0,0 +1,78 @@
+const gql = url =>
+   (query, variables = {}) =>
+      fetch(url, {
+         method: 'POST',
+         headers: {
+            "Content-Type": "application/json",
+            "Accept": "application/json",
+         },
+         body: JSON.stringify({ query, variables })
+      }).then(res => res.json()).then(data => {
+         try {
+            if (!data.data && data.errors) {
+               throw new SyntaxError(JSON.stringify(Object.values(data.errors[0])[0]));
+            } else {
+               return Object.values(data.data)[0]
+            }
+         }
+         catch (e) {
+
+
+            console.log(e);
+         }
+      });
+
+const url = 'http://shop-roles.node.ed.asmer.org.ua/graphql'
+
+const gqlLogin = (login, password) =>
+   gql(`query login($login:String, $password: String){
+     login(login:$login, password: $password)
+}`, { login, password })
+
+
+const gqlRegister = (login, password) => {
+   return gql(url, `mutation register($login: String, $password: String){
+   UserUpsert(user: {login: $login, password: $password}){
+       _id, login, createdAt
+   }
+}`, { login, password })
+}
+
+const gqlCategories = () => {
+   return gql(url,
+      ` query categories{
+                           CategoryFind(query:"[{}]"){
+                           _id name goods {
+                               _id
+                               name
+                               price
+                               description
+                               }
+                         }
+                    }`
+   );
+}
+
+const categoryIdImagesDescription = (id) => {
+   let SearchId = `\\"_id\\":\\"${id}\\"`
+   return gql(
+      url,
+      `
+        query categoryIdImagesDescription{
+         GoodFind(query:"[{${SearchId}}]"){
+            name description
+            images{
+                _id
+                url
+                text
+            }
+        }
+       }
+        `
+
+   )
+}
+console.log(gqlCategories());
+console.log(categoryIdImagesDescription(""));
+console.log(gqlRegister("noname ", "noname"));
+console.log(gqlLogin("noname ", "noname"));