hw_18_04_graph.html 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <header>GQL</header>
  2. <body>
  3. <script>
  4. function gql(url, query, vars) {
  5. let fetchSettings =
  6. {
  7. method: "POST",
  8. headers:
  9. {
  10. "Content-Type": "application/json",
  11. "Accept": "application/json"
  12. },
  13. body: JSON.stringify(
  14. {
  15. query: query,
  16. variables: vars
  17. })
  18. };
  19. return fetch(url, fetchSettings).then(res => res.json());
  20. }
  21. /*
  22. let query =
  23. `
  24. query myGoodFind($where:String){
  25. GoodFind(query:$where){
  26. _id name
  27. }
  28. }
  29. `;
  30. let vars =
  31. {
  32. "where": '[{"name":"/alax/"}]'
  33. };
  34. gql("http://shop-roles.node.ed.asmer.org.ua/graphql", query, vars)
  35. .then(res => console.log(JSON.stringify(res, null, 4)));
  36. */
  37. async function test() {
  38. async function test1() {
  39. const catQuery = `query cats($q: String){
  40. CategoryFind(query: $q){
  41. _id name
  42. }
  43. }`;
  44. const cats = await gql("http://shop-roles.node.ed.asmer.org.ua/graphql", catQuery, { q: "[{}]" });
  45. console.log(cats); //список категорий с _id name и всем таким прочим
  46. const loginQuery = `query login($login:String, $password:String){
  47. login(login:$login, password:$password)
  48. }`;
  49. const token = await gql("http://shop-roles.node.ed.asmer.org.ua/graphql", loginQuery, { login: "Berg", password: "123456789" });
  50. console.log(token);
  51. }
  52. await test1();
  53. }
  54. test();
  55. </script>
  56. </body>