Pārlūkot izejas kodu

can perform basic gql fetch

miskson 3 gadi atpakaļ
vecāks
revīzija
c5b5f74e5f
2 mainītis faili ar 39 papildinājumiem un 0 dzēšanām
  1. 13 0
      hw13-promisePt2/gql/index.html
  2. 26 0
      hw13-promisePt2/gql/script.js

+ 13 - 0
hw13-promisePt2/gql/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>GQL</title>
+</head>
+<body>
+    <h1>GQL</h1>
+    <script src="./script.js"></script>
+</body>
+</html>

+ 26 - 0
hw13-promisePt2/gql/script.js

@@ -0,0 +1,26 @@
+async function gql(url, query, variables={}) {
+    try {
+        let response = fetch(url, {
+            method: 'POST',
+            headers: {
+                'content-type': 'application/json'
+            },
+            body: JSON.stringify({query, variables})
+        })
+        let data = (await response).json()
+        return data
+    } catch(e) {
+        console.warn('Some error: ', e)
+    }
+}
+
+//в консоль 
+(async () => {
+    console.log(await gql(`http://shop-roles.asmer.fs.a-level.com.ua/graphql`, `query cats{
+      CategoryFind(query:"[{}]"){
+        name goods{
+          name
+        }
+      }
+    }`))
+})()