script.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. async function gql(url, query, variables={}) {
  2. try {
  3. let response = fetch(url, {
  4. method: 'POST',
  5. headers: {
  6. 'content-type': 'application/json'
  7. },
  8. body: JSON.stringify({query, variables})
  9. })
  10. let data = (await response).json()
  11. return data
  12. } catch(e) {
  13. console.warn('Some error: ', e)
  14. }
  15. }
  16. //в консоль
  17. (async () => {
  18. console.log(await gql(`http://shop-roles.asmer.fs.a-level.com.ua/graphql`, `query cats{
  19. CategoryFind(query:"[{}]"){
  20. name goods{
  21. name
  22. }
  23. }
  24. }`))
  25. })()
  26. async function getGQL(url) {
  27. return async (query, variables) => {
  28. try{
  29. let response = await fetch(url, {
  30. method: 'POST',
  31. headers: {
  32. "Content-Type": "application/json"
  33. },
  34. body: JSON.stringify({query, variables})
  35. })
  36. let {data} = await response.json()
  37. return data[Object.keys(data)[0]]
  38. } catch(e) {
  39. console.warn('an eror occured', e)
  40. }
  41. }
  42. }
  43. (async () => {
  44. const gql2 = await getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
  45. console.log('from getGQL: ',(await gql2(`
  46. query cats{
  47. CategoryFind(query:"[{}]"){
  48. name goods{
  49. name
  50. }
  51. }
  52. }`
  53. , {login: 'tst', password: '123'})))
  54. })()