123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- async function gql(url, query, variables={}) {
- try {
- let response = fetch(url, {
- method: 'POST',
- headers: {
- 'content-type': 'application/json'
- },
- body: JSON.stringify({query, variables})
- })
- let data = (await response).json()
- return data
- } catch(e) {
- console.warn('Some error: ', e)
- }
- }
- //в консоль
- (async () => {
- console.log(await gql(`http://shop-roles.asmer.fs.a-level.com.ua/graphql`, `query cats{
- CategoryFind(query:"[{}]"){
- name goods{
- name
- }
- }
- }`))
- })()
- async function getGQL(url) {
- return async (query, variables) => {
- try{
- let response = await fetch(url, {
- method: 'POST',
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify({query, variables})
- })
- let {data} = await response.json()
- return data[Object.keys(data)[0]]
- } catch(e) {
- console.warn('an eror occured', e)
- }
- }
- }
-
- (async () => {
- const gql2 = await getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
- console.log('from getGQL: ',(await gql2(`
- query cats{
- CategoryFind(query:"[{}]"){
- name goods{
- name
- }
- }
- }`
- , {login: 'tst', password: '123'})))
- })()
|