main.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const getGQL = url => {
  2. return (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()).then(data => console.log(data))
  10. }
  11. }
  12. let gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql")
  13. let getId = (type, name) => {
  14. let t = type.toLowerCase();
  15. let query;
  16. if (t === "category") {
  17. query = `query fndCtgId($n: String) {
  18. CategoryFind(query: $n){
  19. _id
  20. }
  21. }`
  22. } else if (t === "good") {
  23. query = `query fndCtgId($n: String) {
  24. GoodFind(query: $n){
  25. _id
  26. }
  27. }`
  28. }
  29. let qVariables = {
  30. "n": JSON.stringify([{ "name": name }])
  31. }
  32. gql(query, qVariables)
  33. }
  34. let categoryById = id => {
  35. let query = `query fndcategory($id: String) {
  36. CategoryFind(query: $id){
  37. name goods{
  38. name price images {
  39. url
  40. }
  41. }
  42. }
  43. }`
  44. let qVariables = {
  45. "id": JSON.stringify([{ "_id": id }])
  46. }
  47. gql(query, qVariables)
  48. }
  49. let goodById = id => {
  50. let query = `query fndgood($id: String) {
  51. GoodFind(query: $id){
  52. name price images {
  53. url
  54. }
  55. }
  56. }`
  57. let qVariables = {
  58. "id": JSON.stringify([{ "_id": id }])
  59. }
  60. gql(query, qVariables)
  61. }