12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- const queryCategories = `query categoriesQuery {
- CategoryFind(query:"[{}]") {
- _id
- name
- }
- }`;
- const queryCategory = `query categoryQuery($categoryId:String) {
- CategoryFindOne(query:$categoryId) {
- _id name goods {
- _id
- }
- }
- }`;
- const queryGood = `query goodQuery($goodId:String) {
- GoodFindOne(query:$goodId) {
- _id description images {
- url
- }
- }
- }`;
- const mutationRegistration = `mutation register($login:String, $password:String) {
- UserUpsert(user:{login:$login, password:$password}) {
- _id login createdAt
- }
- }`;
- const queryLogin = `query log($login:String, $password:String) {
- login(login:$login, password:$password)
- }`;
- const categoryIdVal = '6262ca7dbf8b206433f5b3d1';
- const goodIdVal = '62d3099ab74e1f5f2ec1a125';
- const login = 'test462';
- const password = '123123123';
- const url = 'http://shop-roles.node.ed.asmer.org.ua/graphql';
- const checkAuthToken = () => {
- const headers = {
- "Content-Type": "application/json",
- "Accept": "application/json",
- }
- if(localStorage.getItem('authToken')) {
- return {
- ...headers,
- "Authorization": `Bearer ${localStorage.getItem('authToken')}`
- }
- } else {
- return headers
- }
- }
- const getGQL = url => (query, variables= {}) => fetch(url, {
- method: 'POST',
- headers: checkAuthToken(),
- body:JSON.stringify({query, variables})
- }).then(res => res.json())
- .then(data => {
- try {
- if(!data.data && data.errors) {
- throw new SyntaxError(`SyntaxError - ${JSON.stringify(Object.values(data.errors)[0])}`);
- }
- return data.data[Object.keys(data.data)[0]];
- } catch (e) {
- console.error(e);
- }
- });
- /*const gql = getGQL(url);
- gql(queryCategoryFind).then(data => console.log(data));*/
- ;(async () => {
- const gql = getGQL(url);
- const gqlCategory = (_id) => gql(queryCategory, {categoryId: JSON.stringify([{_id}])});
- const gqlGood = (_id) => gql(queryGood, {goodId: JSON.stringify([{_id}])});
- const gqlReg = (login, password) => gql(mutationRegistration, {login, password});
- const gqlLogin = (login, password) => gql(queryLogin, {login, password});
- // категории
- console.log(await gql(queryCategories))
- // категория по id с товарами
- console.log(await gqlCategory(categoryIdVal));
- // товар по id с картинками и описанием
- console.log(await gqlGood(goodIdVal));
- // регистрация
- console.log(await gqlReg(login, password));
- // логин
- console.log(await gqlLogin(login, password));
- })();
|