main.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. let getId = async (type, name) => {
  15. let t = type.toLowerCase();
  16. let query;
  17. if (t === "category") {
  18. query = `query fndCtgId($n: String) {
  19. CategoryFind(query: $n){
  20. _id
  21. }
  22. }`
  23. } else if (t === "good") {
  24. query = `query fndCtgId($n: String) {
  25. GoodFind(query: $n){
  26. _id
  27. }
  28. }`
  29. }
  30. let qVariables = {
  31. "n": JSON.stringify([{ "name": name }])
  32. }
  33. let res = await gql(query, qVariables)
  34. return res
  35. }
  36. let categoryById = async (id) => {
  37. let query = `query fndcategory($id: String) {
  38. CategoryFind(query: $id){
  39. name goods{
  40. name price images {
  41. url
  42. }
  43. }
  44. }
  45. }`
  46. let qVariables = {
  47. "id": JSON.stringify([{ "_id": id }])
  48. }
  49. let res = await gql(query, qVariables)
  50. console.log(res)
  51. return res
  52. }
  53. let goodById = async (id) => {
  54. let query = `query fndgood($id: String) {
  55. GoodFind(query: $id){
  56. name price images {
  57. url
  58. }
  59. }
  60. }`
  61. let qVariables = {
  62. "id": JSON.stringify([{ "_id": id }])
  63. }
  64. let res = await gql(query, qVariables)
  65. return res
  66. }
  67. let addProduct = async ({ name, price, description, imgSrc, categories }) => {
  68. let query = `mutation addGood($product: GoodInput) {
  69. GoodUpsert(good: $product){
  70. _id
  71. }
  72. }`
  73. let qVariables = {
  74. "product": JSON.stringify([{ "name": name, "price": price, "description": description, "images": [{ "url": imgSrc }], "categories": [{ "_id": categories }] }])
  75. }
  76. let res = await gql(query, qVariables)
  77. return res
  78. }
  79. let deleteProdact = async (id) => {
  80. let query = `mutation goodDel($id: GoodInput) {
  81. GoodDelete(good: $id)
  82. }`
  83. let qVariables = {
  84. "id": JSON.stringify([{ "_id": id }])
  85. }
  86. let res = await gql(query, qVariables)
  87. return res
  88. }