main.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const getGQL = url =>
  2. (query, variables) =>
  3. fetch(url , {
  4. method: 'POST',
  5. headers: {
  6. "content-type": "application/json"
  7. },
  8. body: JSON.stringify({query, variables})
  9. }).then(res => res.json())
  10. let gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql")
  11. async function categoryById(id) {
  12. let query = `query CategoryFind($id:String) {
  13. CategoryFind(query:$id) {
  14. name goods {
  15. name
  16. price
  17. description
  18. images {url}
  19. }
  20. }
  21. }`
  22. let variables = {"id": JSON.stringify([{ "_id": id }])} //or {"id": "[{\"_id\":\"id\"}]"}
  23. let res = await gql(query, variables)
  24. console.log(res)
  25. return res
  26. }
  27. categoryById("5dc458985df9d670df48cc47")
  28. async function goodById(id) {
  29. let query = `query GoodFind($id:String) {
  30. GoodFind(query:$id) {
  31. categories {
  32. _id
  33. name
  34. }
  35. name
  36. price
  37. description
  38. createdAt
  39. images {url}
  40. }
  41. }`
  42. let variables = {"id": JSON.stringify([{ "_id": id }])} //or {"id": "[{\"_id\":\"id\"}]"}
  43. let res = await gql(query, variables)
  44. console.log(res)
  45. return res
  46. }
  47. goodById("5e203b5456d8f720513e6cba")
  48. async function goodFind() {
  49. let query = `query GoodFind($q:String) {
  50. GoodFind(query:$q) {
  51. categories {
  52. _id
  53. name
  54. }
  55. _id
  56. name
  57. price
  58. description
  59. createdAt
  60. images {url}
  61. }
  62. }`
  63. let variables = {"q": JSON.stringify([{}])} //or {"q": "[{}]"}
  64. let res = await gql(query, variables)
  65. console.log(res)
  66. return res
  67. }
  68. goodFind()
  69. async function categoryFind() {
  70. let query = `query CategoryFind($q:String) {
  71. CategoryFind(query:$q) {
  72. _id
  73. name
  74. createdAt
  75. goods {
  76. _id
  77. name
  78. price
  79. createdAt
  80. }
  81. }
  82. }`
  83. let variables = {"q": JSON.stringify([{}])} //or {"q": "[{}]"}
  84. let res = await gql(query, variables)
  85. console.log(res)
  86. return res
  87. }
  88. categoryFind()