index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. let gql = (url, query, variables={}) =>
  2. fetch(url, {
  3. method: 'POST',
  4. headers:{
  5. Accept: 'application/json',
  6. "Content-Type": 'application/json'
  7. },
  8. body: JSON.stringify({query, variables})
  9. }).then(res => res.json())
  10. const graphiQL = "http://shop-roles.node.ed.asmer.org.ua/graphql";
  11. let log = (login, password) =>
  12. gql(graphiQL,
  13. `query login($login:String, $password:String){
  14. login(login:$login, password:$password)
  15. }`, {login, password})
  16. let register = (login, password) =>
  17. gql(graphiQL,
  18. `mutation register($login:String, $password:String){
  19. UserUpsert(user:{login:$login, password:$password}){
  20. _id login createdAt
  21. }
  22. }`, {login, password})
  23. let category = () =>
  24. gql(graphiQL,
  25. `query category {
  26. CategoryFind(query: "[{}]"){
  27. _id, name
  28. }
  29. }`)
  30. let catById = () =>
  31. gql(graphiQL,
  32. `query catById {
  33. CategoryFind(query: "[{}]"){
  34. _id name goods {
  35. _id
  36. name
  37. price
  38. description
  39. price
  40. }
  41. }
  42. }`)
  43. (async function gqlResult() {
  44. console.log(await register("levshin95", "123123"))
  45. console.log(await log("levshin95", "123123"));
  46. console.log(await category());
  47. console.log(await catById());
  48. })();