main.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const gql = url =>
  2. (query, variables = {}) =>
  3. fetch(url, {
  4. method: 'POST',
  5. headers: {
  6. "Content-Type": "application/json",
  7. "Accept": "application/json",
  8. },
  9. body: JSON.stringify({ query, variables })
  10. }).then(res => res.json()).then(data => {
  11. try {
  12. if (!data.data && data.errors) {
  13. throw new SyntaxError(JSON.stringify(Object.values(data.errors[0])[0]));
  14. } else {
  15. return Object.values(data.data)[0]
  16. }
  17. }
  18. catch (e) {
  19. console.log(e);
  20. }
  21. });
  22. const url = 'http://shop-roles.node.ed.asmer.org.ua/graphql'
  23. const gqlLogin = (login, password) =>
  24. gql(`query login($login:String, $password: String){
  25. login(login:$login, password: $password)
  26. }`, { login, password })
  27. const gqlRegister = (login, password) => {
  28. return gql(url, `mutation register($login: String, $password: String){
  29. UserUpsert(user: {login: $login, password: $password}){
  30. _id, login, createdAt
  31. }
  32. }`, { login, password })
  33. }
  34. const gqlCategories = () => {
  35. return gql(url,
  36. ` query categories{
  37. CategoryFind(query:"[{}]"){
  38. _id name goods {
  39. _id
  40. name
  41. price
  42. description
  43. }
  44. }
  45. }`
  46. );
  47. }
  48. const categoryIdImagesDescription = (id) => {
  49. let SearchId = `\\"_id\\":\\"${id}\\"`
  50. return gql(
  51. url,
  52. `
  53. query categoryIdImagesDescription{
  54. GoodFind(query:"[{${SearchId}}]"){
  55. name description
  56. images{
  57. _id
  58. url
  59. text
  60. }
  61. }
  62. }
  63. `
  64. )
  65. }
  66. console.log(gqlCategories());
  67. console.log(categoryIdImagesDescription(""));
  68. console.log(gqlRegister("noname ", "noname"));
  69. console.log(gqlLogin("noname ", "noname"));