function getGql (endpoint){ let headers = { 'Content-Type': 'application/json;charset=utf-8', 'Accept': 'application/json' } if ("authToken" in localStorage) { headers.Authorization = "Bearer " + localStorage.authToken } return async function gql(query, variables={}) { let result = await fetch(endpoint, { method: 'POST', headers, body: JSON.stringify({ query, variables }) }).then(res => res.json()) if (("errors" in result) && !("data" in result)) { throw new Error(JSON.stringify(result.errors)) } result = Object.values(result.data)[0] return result } } gql = getGql('http://shop-roles.node.ed.asmer.org.ua/graphql') let queryFindRoots = `query findRoots($q:String) { CategoryFind(query: $q){ _id name parent{ _id name } } }` let variablesFindRoots = { q:JSON.stringify([{parent:null}]) } gql(queryFindRoots , variablesFindRoots ).then(console.log) const actionFindRoots = () => actionPromise("findRoots",gql(queryFindRoots, variablesFindRoots)) let queryCatFindOne = `query categoryFindOne ($q : String) { CategoryFindOne(query: $q){ _id name parent{ _id name } goods{ _id name description price images{ url } } subCategories{ _id name } } } ` let variablesCatFindOne = { q:JSON.stringify([{ _id }]) } gql(queryCatFindOne,variablesCatFindOne).then(console.log) const actionCatFindOne = (_id) => actionPromise("CatFindOne",gql(queryCatFindOne, variablesCatFindOne)) let queryGoodFindOne = `query goodfindOne ($q : String) { GoodFindOne(query: $q){ _id name price description images{ url } } }` let variablesGoodFindOne = { q:JSON.stringify([{ _id }]) } gql(queryGoodFindOne,variablesGoodFindOne).then(console.log) const actionGoodFindOne = (_id) => actionPromise("GoodFindOne",gql(queryGoodFindOne, variablesGoodFindOne)) let mutationRegistr = `mutation registration($login:String, $password: String) { UserUpsert(user : {login:$login, password: $password}){ _id createdAt login } }` let variablesRegistr = { "login" : login , "password" : password } gql( mutationRegistr,variablesRegistr).then(console.log) const actionRegistr = (login,password) => actionPromise("Registr",gql( mutationRegistr, variablesRegistr)) let queryLogin = `query login($login:String,$password:String){ login(login:$login,password:$password) }` let variablesLogin = { login, password } gql(queryLogin,variablesLogin).then(console.log) const actionLogin = (login,password) => actionPromise("Login",gql(queryLogin, variablesLogin)) let queryHistoryOrder = `query historyOrder($order : String){ OrderFind(query: $order){ _id createdAt total owner { _id createdAt login nick } orderGoods{ _id count good { _id createdAt name description price } total } } } ` let variablesHistoryOrder = { q:JSON.stringify([{order : [{}]}]) } gql(queryHistoryOrder,variablesHistoryOrder).then(console.log) const actionHistoryOrder = () => actionPromise("HistoryOrder",gql(queryHistoryOrder, variablesHistoryOrder)) // const NewOrder = (orderGoods) => actionPromise( "NewOrder", gql( `mutation NewOrder($order: OrderInput) { OrderUpsert(order: $order) { _id orderGoods { _id price count total good { name _id price images { url } } } } }`, { order: { orderGoods } } ) )