index.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>GraphQL</title>
  8. <style></style>
  9. </head>
  10. <body>
  11. <script>
  12. const backendURL = "http://shop-roles.node.ed.asmer.org.ua/graphql";
  13. let gql = (url, query, variables = {}) =>
  14. fetch(url, {
  15. method: "POST",
  16. headers: {
  17. Accept: "application/json",
  18. "Content-Type": "application/json",
  19. },
  20. body: JSON.stringify({ query, variables }),
  21. }).then((res) => res.json());
  22. const categoryFind = () =>
  23. gql(
  24. backendURL,
  25. `
  26. query catz{
  27. CategoryFind(query:"[{}]") {
  28. name
  29. }
  30. }
  31. `
  32. );
  33. const registerUser = (login, password) =>
  34. gql(
  35. backendURL,
  36. `
  37. mutation register($login:String, $password:String){
  38. UserUpsert(user: {login:$login, password:$password}){
  39. _id login createdAt
  40. }
  41. }
  42. `,
  43. { login, password }
  44. );
  45. const login = (login, password) =>
  46. gql(
  47. backendURL,
  48. `
  49. query login($login:String, $password:String){
  50. login(login:$login, password:$password)
  51. }
  52. `,
  53. { login, password }
  54. );
  55. const categories = () =>
  56. gql(
  57. backendURL,
  58. `
  59. query categoryFind{
  60. CategoryFind(query:"[{}]"){
  61. _id name goods {
  62. _id
  63. createdAt
  64. name
  65. description
  66. price
  67. }
  68. }
  69. }`
  70. );
  71. const productSearchByIdImgDiscr = (id) => {
  72. let a = `\\"_id\\":\\"${id}\\"`;
  73. return gql(
  74. backendURL,
  75. `
  76. query searchId{
  77. GoodFind(query:"[{${a}}]"){
  78. name description images {
  79. _id
  80. createdAt
  81. text
  82. url
  83. originalFileName
  84. }
  85. }
  86. }`
  87. );
  88. };
  89. (async () => {
  90. console.log("all category:", await categoryFind());
  91. console.log(
  92. "register user:",
  93. await registerUser("illiaKozyr", "qwerty123456")
  94. );
  95. console.log(
  96. "user login:",
  97. await login("illiaKozyr", "qwerty123456")
  98. );
  99. console.log(
  100. "category by id with products:",
  101. await categories()
  102. );
  103. console.log(
  104. "product search by id:",
  105. await productSearchByIdImgDiscr("62c9472cb74e1f5f2ec1a0d3")
  106. );
  107. })();
  108. </script>
  109. </body>
  110. </html>