const getGQL = function (url) { return async function (query, variables) { await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(localStorage.authToken ? {'Authorization': 'Bearer ' + localStorage.authToken} : {}) }, body: JSON.stringify({query, variables}) }) .then(response => response.json()) .then((data) => { if (data.errors && !data.data) { throw new Error(JSON.stringify(data.errors)); } console.log(data.data[Object.keys(data.data)[0]]); return data.data[Object.keys(data.data)[0]]; }); }; }; const gql = getGQL(`http://shop-roles.asmer.fs.a-level.com.ua/graphql`); const catById = (_id) => { return gql(`query catById($q: String){ CategoryFindOne(query: $q){ _id name goods { _id name price images { url } } } }`, {q: JSON.stringify([{_id}])}); }; const getInfoAboutProduct = (_id) => { return gql(`query HzSho{ GoodFind(query:"[{}]"){ _id price description categories{ goods{ name } } } }`, {q: JSON.stringify([{_id}])}); }; const goodFindOne = (_id) => { return gql(` query findTwo{ GoodFindOne(query: "[{}]"){ _id price name } }`, {q: JSON.stringify([{_id}])}); }; const categoryFind = (_id) => { return gql(` query catFind{ CategoryFind(query: "[{}]"){ _id name image{ text url } } }`, {q: JSON.stringify([{_id}])}); }; const getLogin = (login, password) => { return gql(` query NameForMe1($login:String, $password:String){ login(login:$login, password:$password) }`, {login: login, password: password} ); }; (async () => { await catById() await getLogin('lesha', '1305'); await getLogin('кто', 'то'); await goodFindOne(); await getInfoAboutProduct(); await categoryFind(); })();