123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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 = 'test461';
- 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 => data.data[Object.keys(data.data)[0]]);
- /*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));
- })();
|