main.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const getGQL = function (url) {
  2. return async function (query, variables) {
  3. await fetch(url, {
  4. method: 'POST',
  5. headers: {
  6. 'Content-Type': 'application/json',
  7. ...(localStorage.authToken ? {'Authorization': 'Bearer ' + localStorage.authToken} :
  8. {})
  9. },
  10. body: JSON.stringify({query, variables})
  11. })
  12. .then(response => response.json())
  13. .then((data) => {
  14. if (data.errors && !data.data) {
  15. throw new Error(JSON.stringify(data.errors));
  16. }
  17. console.log(data.data[Object.keys(data.data)[0]]);
  18. return data.data[Object.keys(data.data)[0]];
  19. });
  20. };
  21. };
  22. const gql = getGQL(`http://shop-roles.asmer.fs.a-level.com.ua/graphql`);
  23. const catById = (_id) => {
  24. return gql(`query catById($q: String){
  25. CategoryFindOne(query: $q){
  26. _id name goods {
  27. _id name price images {
  28. url
  29. }
  30. }
  31. }
  32. }`, {q: JSON.stringify([{_id}])});
  33. };
  34. const getInfoAboutProduct = (_id) => {
  35. return gql(`query HzSho{
  36. GoodFind(query:"[{}]"){
  37. _id price description
  38. categories{
  39. goods{
  40. name
  41. }
  42. }
  43. }
  44. }`, {q: JSON.stringify([{_id}])});
  45. };
  46. const goodFindOne = (_id) => {
  47. return gql(`
  48. query findTwo{
  49. GoodFindOne(query: "[{}]"){
  50. _id price name
  51. }
  52. }`, {q: JSON.stringify([{_id}])});
  53. };
  54. const categoryFind = (_id) => {
  55. return gql(`
  56. query catFind{
  57. CategoryFind(query: "[{}]"){
  58. _id name image{
  59. text url
  60. }
  61. }
  62. }`, {q: JSON.stringify([{_id}])});
  63. };
  64. const getLogin = (login, password) => {
  65. return gql(` query NameForMe1($login:String, $password:String){
  66. login(login:$login, password:$password)
  67. }`, {login: login, password: password}
  68. );
  69. };
  70. (async () => {
  71. await catById()
  72. await getLogin('lesha', '1305');
  73. await getLogin('кто', 'то');
  74. await goodFindOne();
  75. await getInfoAboutProduct();
  76. await categoryFind();
  77. })();