Browse Source

getGQL done

Vladimir 3 years ago
parent
commit
d5b67fcb31
2 changed files with 72 additions and 0 deletions
  1. 12 0
      getGQL/index.html
  2. 60 0
      getGQL/main.js

+ 12 - 0
getGQL/index.html

@@ -0,0 +1,12 @@
+<!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>

+ 60 - 0
getGQL/main.js

@@ -0,0 +1,60 @@
+let getGQL = function(url) {
+    return async function(data) {
+        let res = await fetch(url, {
+            method: "POST",
+            headers: {
+                "content-type": "application/json"
+            },
+            body:  JSON.stringify(data)
+        });
+
+        return res.json();
+    };
+};
+
+let gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql");
+
+let login = async function(login, password) {
+    let data = {
+        query: "query log($login: String, $password: String) {login(login: $login, password: $password)}",
+        variables: {
+            login: login,
+            password: password
+        }
+    };
+
+    let res = await gql(data);
+    let token = res["data"]["login"];
+
+    return token;
+};
+
+let catByID = async function(id) {
+    let data = {
+        query: "query cat($query: String) {CategoryFindOne(query: $query) {_id name image {url}}}",
+        variables: {
+            query: JSON.stringify([{"_id": id}])
+        }
+    };
+
+    let res = await gql(data);
+
+    return res;
+};
+
+let goodByID = async function(id) {
+    let data = {
+        query: "query findGood($goodQuery: String) {GoodFindOne(query:$goodQuery) {_id name}}",
+        variables: {
+            goodQuery: JSON.stringify([{"_id": id}])
+        }
+    };
+
+    let res = await gql(data);
+
+    return res;
+};
+
+login("user789", "123123");
+catByID("5dc49f4d5df9d670df48cc64");
+goodByID("5dcad32b6d09c45440d14d0f");