const getGQL = url => (query, variables) => 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 reg = async(login, password) => { let query = `mutation reg($login:String, $password:String) { UserUpsert(user:{ login: $login, password: $password }){ _id } }` let variables = {"login":login, "password":password} let res = await gql(query, variables) console.log(res) return res } //логин let login = async(login, password) => { let query = `query login($login:String, $password:String) { login(login: $login, password: $password) }` let variables = {"login":login, "password":password} let token = await gql(query, variables) console.log(token) localStorage.authToken = token.data.login } //отправка заказа let newOrder = async(obj) => { let option = Object.entries(obj) let orderGoods = [] for (let key of option) { let i = { "count": key[1], "good": {"_id": key[0]} } orderGoods.push(i) } let query = `mutation newOrder($order:OrderInput) { OrderUpsert(order:$order) { _id } }` let variables = { "order": {"orderGoods": orderGoods} } let res = await gql(query, variables) console.log(res) return res } //получениe всех заказов let getOrders = async() => { let query = `query orders { OrderFind(query:"[{}]") { _id total orderGoods { good { _id name } count price total } } }` let res = await gql(query) console.log(res) return res } //получениe одного заказа по его _id let orderById = async(id) => { let query = `query orderId($id:String) { OrderFind(query:$id) { createdAt total orderGoods { good { _id name } count total price } } }` let variables = { "id": JSON.stringify([{ "_id": id }]) } let res = await gql(query, variables) console.log(res) return res }