main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const getGQL = url =>
  2. (query, variables) =>
  3. 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. let gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql")
  12. let reg = async(login,password) => {
  13. let query = `mutation reg($l:String , $p:String) {
  14. UserUpsert(user:{
  15. login:$l ,
  16. password:$p
  17. }){
  18. _id
  19. }
  20. }`
  21. let qVariables = {
  22. "l": login,
  23. "p": password
  24. }
  25. let result = await gql(query,qVariables)
  26. return result
  27. }
  28. reg ('v3rtex' , 'senior')
  29. let log = async(login , password) => {
  30. let query = ` query log($l:String , $p:String){
  31. login(login:$l, password:$p)
  32. }`
  33. let qVariables = {
  34. "l": login,
  35. "p": password
  36. }
  37. let token = await gql(query,qVariables)
  38. console.log(token)
  39. localStorage.authToken = token.data.login
  40. }
  41. log('v3rtex' , 'senior')
  42. let newOrder = async(obj) => {
  43. let option = Object.entries(obj)
  44. let orderGoods = []
  45. for (let key of option) {
  46. let iteration = {
  47. "count": key[1],
  48. "good":{"_id":key[0]}
  49. }
  50. orderGoods.push(iteration)
  51. }
  52. let query = `mutation newOrder($order:OrderInput) {
  53. OrderUpsert(order:$order){
  54. _id
  55. }
  56. }`
  57. let qVariables = {
  58. "order": {
  59. "orderGoods": orderGoods}
  60. }
  61. let result = await gql(query,qVariables)
  62. console.log(result)
  63. return result
  64. }
  65. newOrder({ "5dc45d0b5df9d670df48cc4b":10})
  66. let allOrders = async() => {
  67. let res = await gql (`query orders {
  68. OrderFind(query:"[{}]"){
  69. _id total
  70. orderGoods{
  71. good{
  72. name
  73. }
  74. price
  75. count
  76. total
  77. }
  78. }
  79. }`)
  80. console.log(res)
  81. return res
  82. }
  83. allOrders()
  84. let orderById = async(id) => {
  85. let query = `query orderID {
  86. OrderGoodFindOne(query:"[{}]"){
  87. _id
  88. good{
  89. _id
  90. name
  91. }
  92. }
  93. }`
  94. let qVariables = {id}
  95. let res = await gql(query,qVariables)
  96. console.log(res)
  97. return res
  98. }
  99. orderById("611f904bc456ed5e4d9d682b")
  100. async function goodFind() {
  101. await gql(`query {
  102. GoodFind(query: "[{}]"){
  103. _id
  104. name
  105. price
  106. images{
  107. url
  108. }
  109. }
  110. }`
  111. )}
  112. goodFind()
  113. async function categoryFind() {
  114. await gql(`query {
  115. CategoryFind(query: "[{}]"){
  116. _id
  117. name
  118. goods {
  119. _id name
  120. }
  121. }
  122. }`).then(result => console.log(result))
  123. }
  124. categoryFind()