Ver código fonte

can perform basic gql fetch

miskson 2 anos atrás
pai
commit
c5b5f74e5f
2 arquivos alterados com 39 adições e 0 exclusões
  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
+        }
+      }
+    }`))
+})()