main.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const getGQL = url => {
  2. return (query, variables) => {
  3. return fetch(url, {
  4. method: 'POST',
  5. headers: {
  6. "content-type": "application/json",
  7. ...(localStorage.authToken ? { Authorization: "Bearer " + localStorage.authToken } : {})
  8. },
  9. body: JSON.stringify({ query, variables }),
  10. }).then(res => res.json())
  11. }
  12. }
  13. let gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql")
  14. //функция для регистрации
  15. let reg = async (login, password) => {
  16. let query = `mutation reg($l: String, $p: String) {
  17. UserUpsert(user: {
  18. login: $l,
  19. password: $p
  20. } ) {
  21. _id
  22. }
  23. }`
  24. let qVariables = {
  25. "l": login,
  26. "p": password
  27. }
  28. let res = await gql(query, qVariables)
  29. return res
  30. }
  31. //функция для логина
  32. let log = async (login, password) => {
  33. let query = `query log($l: String, $p: String) {
  34. login(login: $l, password: $p)
  35. }`
  36. let qVariables = {
  37. "l": login,
  38. "p": password
  39. }
  40. let token = await gql(query, qVariables)
  41. console.log(token)
  42. localStorage.authToken = token.data.login
  43. }
  44. //функция для отправки заказа. Принимает объект вида:{_id1: количество, _id2: количество}
  45. let newOrder = async (obj) => {
  46. let option = Object.entries(obj);
  47. let orderGoods = [];
  48. for (let key of option) {
  49. let i = {
  50. "count": key[1],
  51. "good": { "_id": key[0] }
  52. }
  53. orderGoods.push(i);
  54. }
  55. let query = `mutation sndOrder($order: OrderInput) {
  56. OrderUpsert(order: $order) {
  57. _id createdAt total
  58. }
  59. }`
  60. let qVariables = {
  61. "order": {
  62. "orderGoods": orderGoods
  63. }
  64. }
  65. let res = await gql(query, qVariables)
  66. console.log(res)
  67. return res
  68. }
  69. //функция для получения всех заказов
  70. let getOrders = async () => {
  71. let query = `query ordersfnd {
  72. OrderFind(query: "[{}]"){
  73. _id total owner{
  74. _id
  75. }
  76. orderGoods{
  77. good{
  78. _id
  79. name
  80. }
  81. count total
  82. }
  83. }
  84. }`
  85. let res = await gql(query)
  86. console.log(res)
  87. return res
  88. }
  89. //функция для получения одного заказа по _id
  90. let orderById = async (id) => {
  91. let query = `query orderfnd($id: String){
  92. OrderFind(query: $id){
  93. createdAt total owner{
  94. _id
  95. }
  96. orderGoods{
  97. good{
  98. _id
  99. name
  100. }
  101. count total
  102. }
  103. }
  104. }`
  105. let qVariables = {
  106. "id": JSON.stringify([{ "_id": id }])
  107. }
  108. let res = await gql(query, qVariables)
  109. console.log(res)
  110. return res
  111. }