main.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const getGQL = url =>
  2. (query, variables = {}) => fetch(url, {
  3. method: 'POST',
  4. headers: {
  5. ...(localStorage.authToken ? {"Authorization": "Bearer " + localStorage.authToken} : {}),
  6. "Content-Type": "application/json",
  7. },
  8. body: JSON.stringify({query, variables})
  9. }).then(res => res.json())
  10. .then(data => { console.log(data);
  11. if (data.errors && !data.data)
  12. throw new Error(JSON.stringify(data.errors))
  13. return data.data[Object.keys(data.data)[0]]
  14. })
  15. const gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql');
  16. const autoriz = () =>
  17. gql(`query autoriz ($login:String, $password:String){
  18. login(login:$login, password:$password)
  19. }`,
  20. {
  21. "login": "vika",
  22. "password": "2021"
  23. })
  24. async function autorization () {
  25. let token = await autoriz();
  26. localStorage.setItem ("authToken", token);
  27. }
  28. autorization();
  29. let _id = "5dcad1316d09c45440d14d07";
  30. async function orderGood (_id) {
  31. const oderUpsert = await gql(`mutation order{
  32. OrderUpsert(order:{
  33. orderGoods:[
  34. {count:3, good:{_id: "${_id}"}
  35. }
  36. ]
  37. }) {
  38. total _id
  39. }}
  40. `)
  41. }
  42. orderGood(_id);
  43. let _idOderUpsert = "61a9f1c8c750c12ba6ba41d9";
  44. async function orderById (_id) {
  45. await gql(`query order($q1:String) {
  46. OrderFindOne(query:$q1){
  47. _id total,
  48. }
  49. }`, {"q1": JSON.stringify([{_id}])})
  50. }
  51. orderById(_idOderUpsert);
  52. async function catById(_){
  53. let category = await gql(`query category {
  54. CategoryFind(query:"[{}]"){
  55. _id
  56. }
  57. }`);
  58. const oneCategory = category[1]._id; console.log(oneCategory)
  59. await gql(`query catByOne ($query:String){
  60. CategoryFindOne(query:$query){
  61. name goods{
  62. _id name
  63. }
  64. }
  65. }`, {"query": JSON.stringify([{_id :oneCategory}])})
  66. }
  67. catById();
  68. const goodId = "5dc87cb90e36db246e3049b0";
  69. const backURL = 'http://shop-roles.asmer.fs.a-level.com.ua';
  70. async function getImageGood (_id) {
  71. const data = await gql(`query imageFind($image:String){
  72. ImageFind(query: $image){
  73. url good {
  74. _id name
  75. }
  76. }
  77. }`, {
  78. "image": JSON.stringify([{_id}])
  79. })
  80. document.body.innerHTML =`<img src= "${backURL}/${data[0].url}" alt="альтернативный текст" />`
  81. }
  82. getImageGood(goodId);