12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- let url = 'http://shop-roles.asmer.fs.a-level.com.ua/graphql';
- const getGQL = url => (query,variables = {}) =>
- fetch(url, {
- method : 'POST',
- headers : {
- 'Content-Type' : 'application/json',
- 'Accept' : 'application/json',
- ...(localStorage.authToken ? {"Authorization": "Bearer " + localStorage.authToken} : {})
- },
- body : JSON.stringify({query,variables})
- }
- ).then(res => res.json())
- .then((data)=>{
- if(data.errors && !data.data){
- throw new Error(JSON.stringify(data.errors))
- } else {
- let ar = Object.values(data.data)
- return ar[0]
- }
- }).then((data) => data);
- const gql = getGQL(url);
- async function login(log,pass){
- let token = await gql(`query login($login:String, $password:String){
- login(login:$login, password : $password)
- }`, {login : log, password : pass});
- localStorage.authToken = await token;
- }
- async function register(log,pass){
- let data = await gql( `mutation register($login: String, $password: String) {
- UserUpsert(user: {login: $login, password: $password}) {
- _id
- login
- }
- }`, {login : log, password : pass});
- console.log(data);
- }
- async function findCat(id){ // + все гуды этой категории (айди и имя)
- let data = await gql( `query findCatOne($q1: String) {
- CategoryFindOne(query: $q1) {
- name
- _id
- goods {
- _id
- name
- }
- }
- }`, {q1 : JSON.stringify([{_id: id}])});
- console.log(data);
- }
- async function newOrder(){
- let data = await gql( `mutation newOrder($order: OrderInput) {
- OrderUpsert(order: $order) {
- _id
- total
- }
- }`, {order :{
- orderGoods: [
- {count: 2, good: {_id: "5dc4a3e15df9d670df48cc6b"}},
- {count: 3, good: {_id: "5e247bbb56d8f720513e6ce2"}}
- ]
- }})
- console.log(data) ///=====>{_id: '61db3220c750c12ba6ba4fa4', total: 23997}
- }
- async function findGoodId(id){
- let data = await gql(`query findGood($goodId: String) {
- GoodFindOne(query: $goodId) {
- name
- price
- }
- }`, {goodId : JSON.stringify([{_id: id}])})
- console.log(data) //=====>>>{name: 'Морс смородиновый (1 л)', price: 60}
- }
- register('qwewe12334', '445224')
- login("user24049312", "240493");
- findCat("5dc94bd00e36db246e3049ee");
- newOrder();
- findGoodId("5dcac4ce6d09c45440d14cf9");
|