gql.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //gql
  2. {
  3. async function gql(url, query, options) {
  4. let settings = {
  5. method: "POST",
  6. headers:{
  7. "Content-Type": "application/json",
  8. "Accept": "application/json"
  9. },
  10. body: JSON.stringify({query: query, variables: options})
  11. }
  12. const res = await fetch(url, settings)
  13. return await res.json()
  14. }
  15. async function login() {
  16. const catQuery = `query cats($q: String){
  17. CategoryFind(query: $q){
  18. _id name
  19. }
  20. }`
  21. const cats = await gql("http://shop-roles.node.ed.asmer.org.ua/graphql", catQuery, {q: "[{}]"})
  22. console.log(cats) //список категорий с _id name и всем таким прочим
  23. const loginQuery = `query login($login:String, $password:String){
  24. login(login:$login, password:$password)
  25. }`
  26. const token = await gql("http://shop-roles.node.ed.asmer.org.ua/graphql", loginQuery ,{login: "test457", password: "123123"})
  27. console.log(token)
  28. }
  29. login()
  30. }