Browse Source

HW<15> done

Vitalii Polishchuk 3 years ago
parent
commit
a4f468a276
1 changed files with 72 additions and 0 deletions
  1. 72 0
      js/15-js-graphql/main.js

+ 72 - 0
js/15-js-graphql/main.js

@@ -0,0 +1,72 @@
+const getGQL = url => {
+    return (query, variables) => {
+        fetch(url, {
+            method: 'POST',
+            headers: {
+                "content-type": "application/json"
+            },
+            body: JSON.stringify({ query, variables })
+        }).then(res => res.json()).then(data => console.log(data))
+    }
+}
+
+let gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql")
+
+
+let getId = (type, name) => {
+    let t = type.toLowerCase();
+    let query;
+    if (t === "category") {
+        query = `query fndCtgId($n: String) {
+            CategoryFind(query: $n){
+              _id
+            }
+          }`
+    } else if (t === "good") {
+        query = `query fndCtgId($n: String) {
+            GoodFind(query: $n){
+              _id
+            }
+          }`
+    }
+
+    let qVariables = {
+        "n": JSON.stringify([{ "name": name }])
+    }
+
+    gql(query, qVariables)
+}
+
+let categoryById = id => {
+    let query = `query fndcategory($id: String) {
+        CategoryFind(query: $id){
+          name goods{
+              name price images {
+                  url
+              }
+          }
+        }
+      }`
+
+    let qVariables = {
+        "id": JSON.stringify([{ "_id": id }])
+    }
+
+    gql(query, qVariables)
+}
+
+let goodById = id => {
+    let query = `query fndgood($id: String) {
+        GoodFind(query: $id){
+          name price images {
+              url
+          }
+        }
+      }`
+
+    let qVariables = {
+        "id": JSON.stringify([{ "_id": id }])
+    }
+
+    gql(query, qVariables)
+}