123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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
- }
|