script.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. function getGQL(url) {
  2. return async (query, variables) => {
  3. let options = {
  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. }
  11. try {
  12. let response = await fetch(url, options)
  13. let { data } = await response.json()
  14. return data[Object.keys(data)[0]]
  15. } catch (e) {
  16. throw new Error('an eror occured', e)
  17. }
  18. }
  19. }
  20. async function getCathegoryById(_id) {
  21. let gql = await getGQL(endpoint)
  22. return await gql(`
  23. query catById($query:String){
  24. CategoryFindOne(query:$query){
  25. name
  26. goods{
  27. _id name
  28. }
  29. }
  30. }`,
  31. { query: JSON.stringify([{ _id }]) }
  32. )
  33. }
  34. async function loginAndToken(log, pass) {
  35. let gql = await getGQL(endpoint)
  36. return await gql(`
  37. query log($login:String, $password:String) {
  38. login(login: $login, password: $password)
  39. }`,
  40. { login: log, password: pass }
  41. )
  42. }
  43. async function registerUser(log, pass, nick = 'user') {
  44. let gql = await getGQL(endpoint)
  45. return await gql(`
  46. mutation register($login:String, $password:String, $nick:String) {
  47. UserUpsert(
  48. user: { login: $login, password: $password, nick: $nick }
  49. ){
  50. nick login _id
  51. }
  52. }`,
  53. { login: log, password: pass, nick: nick }
  54. )
  55. }
  56. async function goodsList() {
  57. let gql = await getGQL(endpoint)
  58. return await gql(`
  59. query {
  60. GoodFind(query: "[{}]") {
  61. _id name categories{
  62. _id name
  63. }
  64. }
  65. }`
  66. )
  67. }
  68. async function ordersList() {
  69. let gql = await getGQL(endpoint)
  70. return await gql(`
  71. query {
  72. OrderFind(query:"[{}]"){
  73. _id total orderGoods{
  74. count good{
  75. _id name
  76. }
  77. owner{
  78. _id login nick
  79. }
  80. }
  81. }
  82. }
  83. `)
  84. }
  85. async function newOrder(quantity, _id) {
  86. let gql = await getGQL(endpoint)
  87. return await gql(`
  88. mutation newOrder($count:Int!, $id:ID){
  89. OrderUpsert(
  90. order:{
  91. orderGoods: [ {count: $count, good:{_id: $id}} ]
  92. }
  93. ) { _id createdAt total }
  94. }`,
  95. { count: quantity, id: _id }
  96. )
  97. }
  98. async function getOrderById(_id) {
  99. let gql = await getGQL(endpoint)
  100. return await gql(`
  101. query order ($query:String) {
  102. OrderFindOne(query:$query) {
  103. _id
  104. total
  105. orderGoods {
  106. count
  107. good {
  108. name
  109. }
  110. }
  111. }
  112. }`,
  113. { query: JSON.stringify([{ _id }]) }
  114. )
  115. }
  116. async function getUserById(_id) {
  117. let gql = await getGQL(endpoint)
  118. return await gql(`
  119. query findUserById($query:String){
  120. UserFindOne(query:$query) {
  121. _id nick login
  122. }
  123. }`,
  124. { query: JSON.stringify([{ _id }]) }
  125. )
  126. }
  127. let endpoint = 'http://shop-roles.asmer.fs.a-level.com.ua/graphql';
  128. (async () => {
  129. localStorage.authToken = ''
  130. //Тут вставить какой-то кучерявый ник что бы зарегало юзера else = null
  131. console.log('New user', await registerUser("OLDBOY1338", "123", "asdasd"))
  132. localStorage.authToken = await loginAndToken("OLDBOY228", "3321")
  133. console.log('getCathegoryById', await getCathegoryById("5dc94bd00e36db246e3049ee")) //id для питсы
  134. console.log('All goods', await goodsList()) //список всех товаров + категории
  135. console.log('All orders', await ordersList())
  136. console.log('Make order', await newOrder(3, "5dc888d20e36db246e3049c5"))
  137. console.log('Get order by id', await getOrderById("61aa7fb6c750c12ba6ba41fb"))
  138. console.log('Get user by id', await getUserById("61aa9c7ac750c12ba6ba4238")) //айдишник OLDBOY1338
  139. })()