hw.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. const getGQL = (url) => async (query, variables) => {
  2. try {
  3. const token = localStorage.token ? localStorage.token : '';
  4. const res = await fetch(url, {
  5. method: 'POST',
  6. headers: {
  7. 'Content-Type': 'application/json',
  8. Authorization: 'Bearer ' + token,
  9. },
  10. body: JSON.stringify({ query, variables }),
  11. });
  12. return res.json();
  13. } catch (e) {
  14. return e;
  15. }
  16. };
  17. const gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql');
  18. const loginGQL = async (login, password) => {
  19. try {
  20. const { data } = await gql(
  21. `
  22. query log($login:String, $password:String){
  23. login(login:$login, password:$password)
  24. }`,
  25. { login, password }
  26. );
  27. const token = data.login;
  28. if (token) localStorage.token = token;
  29. return token;
  30. } catch (e) {
  31. console.error(e);
  32. }
  33. };
  34. const registerGQL = async (login, password) => {
  35. try {
  36. const { data } = await gql(
  37. `mutation register($login:String, $password:String){
  38. UserUpsert(user: {login:$login, password:$password}){
  39. _id,login
  40. }
  41. }`,
  42. { login, password }
  43. );
  44. return data.UserUpsert;
  45. } catch (e) {
  46. console.error(e);
  47. }
  48. };
  49. const categoriesGQL = async () => {
  50. try {
  51. const { data } = await gql(
  52. `query allCategories{
  53. CategoryFind(query:"[{}]"){
  54. _id,
  55. name,
  56. createdAt
  57. }
  58. }`,
  59. {}
  60. );
  61. return data.CategoryFind;
  62. } catch (e) {
  63. console.error(e);
  64. }
  65. };
  66. const goodsGQL = async () => {
  67. try {
  68. const { data } = await gql(
  69. `query allGoods{
  70. GoodFind(query:"[{}]"){
  71. _id
  72. name,
  73. createdAt,
  74. price,
  75. images {
  76. url
  77. }
  78. }
  79. }`,
  80. {}
  81. );
  82. return data.GoodFind;
  83. } catch (e) {
  84. console.error(e);
  85. }
  86. };
  87. const categoryById = async (_id) => {
  88. try {
  89. const { data } = await gql(
  90. `query categoryById($id:String){
  91. CategoryFindOne(query:$id){
  92. _id,name,createdAt,
  93. goods {
  94. _id,createdAt,name
  95. }
  96. },
  97. }`,
  98. { id: JSON.stringify([{ _id }]) }
  99. );
  100. return data.CategoryFindOne;
  101. } catch (e) {
  102. console.error(e);
  103. }
  104. };
  105. const goodById = async (_id) => {
  106. try {
  107. const { data } = await gql(
  108. `query findById($id:String){
  109. GoodFindOne (query:$id){
  110. _id createdAt name price description images {
  111. url
  112. }
  113. }
  114. }`,
  115. { id: JSON.stringify([{ _id }]) }
  116. );
  117. return data.GoodFindOne;
  118. } catch (e) {
  119. console.error(e);
  120. }
  121. };
  122. const ordersGQL = async () => {
  123. try {
  124. const { data } = await gql(
  125. `query orders {
  126. OrderFind(query:"[{}]")
  127. {
  128. _id
  129. createdAt
  130. total
  131. orderGoods {
  132. price
  133. count
  134. total
  135. good {
  136. name
  137. }
  138. }
  139. }
  140. }`,
  141. {}
  142. );
  143. return data.OrderFind;
  144. } catch (e) {
  145. console.error(e);
  146. }
  147. };
  148. const orderById = async (_id) => {
  149. try {
  150. const { data } = await gql(
  151. `query findById($id:String){
  152. OrderFindOne (query:$id){
  153. _id createdAt
  154. }
  155. }`,
  156. { id: JSON.stringify([{ _id }]) }
  157. );
  158. return data.OrderFindOne;
  159. } catch (e) {
  160. console.error(e);
  161. }
  162. };
  163. const makeOrderById = async (id) => {
  164. try {
  165. const { data } = await gql(
  166. ` mutation makeOrder($id:ID){
  167. OrderUpsert(order:{
  168. orderGoods:[
  169. {count:1,good:{_id:$id}}
  170. ]
  171. }){
  172. _id,createdAt,total
  173. }
  174. }`,
  175. { id }
  176. );
  177. return data.OrderUpsert;
  178. } catch (e) {
  179. console.error(e);
  180. }
  181. };
  182. export {
  183. loginGQL,
  184. registerGQL,
  185. categoriesGQL,
  186. goodsGQL,
  187. categoryById,
  188. goodById,
  189. ordersGQL,
  190. orderById,
  191. makeOrderById,
  192. };