Browse Source

login request working.others not.

miskson 3 years ago
parent
commit
0cdd51b9fc
2 changed files with 94 additions and 0 deletions
  1. 13 0
      hw14-gql-requests/index.htm
  2. 81 0
      hw14-gql-requests/script.js

+ 13 - 0
hw14-gql-requests/index.htm

@@ -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>gql requests</title>
+</head>
+<body>
+    <h1>gql requests</h1>
+    <script src="./script.js"></script>
+</body>
+</html>

+ 81 - 0
hw14-gql-requests/script.js

@@ -0,0 +1,81 @@
+async function getGQL(url) {
+    return async (query, variables) => {
+        let options = {
+            method: 'POST',
+            headers: {
+                "Content-Type": "application/json",
+            },
+            body: JSON.stringify({ query, variables })
+        }
+
+        if(localStorage.authToken) {
+            options.headers.Authorization = `Bearer ${localStorage.authToken}`
+        }
+
+        console.log(options.headers)
+
+        try {
+            let response = await fetch(url, options)
+            let { data } = await response.json()
+            console.log('data reseved', data)
+            return data[Object.keys(data)[0]]
+        } catch (e) {
+            console.warn('an eror occured', e)
+        }
+    }
+}
+
+(async () => {
+    const gql2 = await getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
+    console.log('from getGQL: ', (await gql2(`
+          query cats{
+            CategoryFind(query:"[{}]"){
+              name goods{
+                name
+              }
+            }
+          }`
+        , { login: 'tst', password: '123' })))
+})()
+
+let _id = "61a762f9c750c12ba6ba40aa"
+
+async function catById(_id) {
+    let gql = await getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
+    let data = await gql(`query catById($query:String){
+                    CategoryFindOne(query:$query){
+                        name goods{
+                        _id name
+                        }
+                    }
+                }`, { query: JSON.stringify([{ _id }]) })
+    return data
+}
+
+async function loginAndToken(log, pass) {
+    let gql = await getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
+    let data =  await gql(`
+        query log($login:String, $password:String) {
+            login(login: $login, password: $password)
+        }`, {login:  log, password: pass})
+    return data
+}
+
+async function registerUser(obj) {
+    let gql = await getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
+    let data = await gql(`
+        mutation register($login:String, $password:String, $nick:String) {
+            UserUpsert(
+            user: { login: $login, password: $password }){
+                login _id 
+            }
+        }`, {user: obj})
+    return data
+}
+
+(async () => {
+    localStorage.authToken = await loginAndToken("OLDBOY228", "3321") 
+    console.log('info token', localStorage.authToken)
+    console.log('idis', await catById(_id))
+    console.log('New user', await registerUser({login:"123F11F", password:"123"}))
+})()