const getGQL = url => { return (query, variables) => { return fetch(url, { method: 'POST', headers: { "content-type": "application/json", ...(localStorage.authToken ? { Authorization: "Bearer " + localStorage.authToken } : {}) }, body: JSON.stringify({ query, variables }), }).then(res => res.json()) } } let gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql") let getId = async (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 }]) } let res = await gql(query, qVariables) return res } let categoryById = async (id) => { let query = `query fndcategory($id: String) { CategoryFind(query: $id){ name goods{ name price images { url } } } }` let qVariables = { "id": JSON.stringify([{ "_id": id }]) } let res = await gql(query, qVariables) console.log(res) return res } let goodById = async (id) => { let query = `query fndgood($id: String) { GoodFind(query: $id){ name price images { url } } }` let qVariables = { "id": JSON.stringify([{ "_id": id }]) } let res = await gql(query, qVariables) return res } let addProduct = async ({ name, price, description, imgSrc, categories }) => { let query = `mutation addGood($product: GoodInput) { GoodUpsert(good: $product){ _id } }` let qVariables = { "product": JSON.stringify([{ "name": name, "price": price, "description": description, "images": [{ "url": imgSrc }], "categories": [{ "_id": categories }] }]) } let res = await gql(query, qVariables) return res } let deleteProdact = async (id) => { let query = `mutation goodDel($id: GoodInput) { GoodDelete(good: $id) }` let qVariables = { "id": JSON.stringify([{ "_id": id }]) } let res = await gql(query, qVariables) return res }