Browse Source

'getgql' function written

miskson 3 years ago
parent
commit
196bdd6d70
1 changed files with 32 additions and 1 deletions
  1. 32 1
      hw13-promisePt2/gql/script.js

+ 32 - 1
hw13-promisePt2/gql/script.js

@@ -23,4 +23,35 @@ async function gql(url, query, variables={}) {
         }
         }
       }
       }
     }`))
     }`))
-})()
+})()
+
+async function getGQL(url) {
+  return async (query, variables) => {
+    try{
+      let response = await fetch(url, {
+        method: 'POST',
+        headers: {
+            "Content-Type": "application/json"
+        },
+        body: JSON.stringify({query, variables})
+      })
+      let {data} = await response.json()
+      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'})))
+})()