|
@@ -1 +1,111 @@
|
|
|
'use strict';
|
|
|
+
|
|
|
+const getGQL = (url) => async (query, variables) => {
|
|
|
+ try {
|
|
|
+ const token = localStorage.getItem('token');
|
|
|
+ const Authorization = token ? `Bearer ${token}` : '';
|
|
|
+ const res = await fetch(url, {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ Authorization,
|
|
|
+ },
|
|
|
+ body: JSON.stringify({ query, variables }),
|
|
|
+ });
|
|
|
+ return res.json();
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql');
|
|
|
+
|
|
|
+const loginGQL = async (login, password) => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `
|
|
|
+query log($login:String, $password:String){
|
|
|
+ login(login:$login, password:$password)
|
|
|
+ }`,
|
|
|
+ { login, password }
|
|
|
+ );
|
|
|
+ const token = data.login;
|
|
|
+ if (token) localStorage.setItem('token', token);
|
|
|
+ return token;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const registerGQL = async (login, password) => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `mutation register($login:String, $password:String){
|
|
|
+ UserUpsert(user: {login:$login, password:$password}){
|
|
|
+ _id,login
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ { login, password }
|
|
|
+ );
|
|
|
+ return data.UserUpsert;
|
|
|
+ } catch (e) {
|
|
|
+ console.log(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const categoriesGQL = async () => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `query allCategories{
|
|
|
+ CategoryFind(query:"[{}]"){
|
|
|
+ _id,
|
|
|
+ name,
|
|
|
+ createdAt
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ {}
|
|
|
+ );
|
|
|
+ return data.CategoryFind;
|
|
|
+ } catch (e) {
|
|
|
+ console.log(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const categoryById = async (_id) => {
|
|
|
+ try {
|
|
|
+ const data = await gql(
|
|
|
+ `query categoryById($id:String){
|
|
|
+ CategoryFindOne(query:$id){
|
|
|
+ name
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ { id: JSON.stringify([{ _id }]) }
|
|
|
+ );
|
|
|
+ return data.data.CategoryFindOne;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const goodById = async (_id) => {
|
|
|
+ try {
|
|
|
+ const data = await gql(
|
|
|
+ `query GoodFindOne($id:String){
|
|
|
+ CategoryFindOne(query:$id){
|
|
|
+ name,
|
|
|
+ _id
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ { id: JSON.stringify([{ _id }]) }
|
|
|
+ );
|
|
|
+ return data.data.CategoryFindOne;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+console.log(await loginGQL('test2021121', 'test2021121'), 'loginGQL');
|
|
|
+console.log(await registerGQL('1234332121', '123'), 'registerGQL');
|
|
|
+console.log(await categoriesGQL(), 'categoriesGQL');
|
|
|
+console.log(await categoryById('5dc458985df9d670df48cc47'), 'categoryById');
|
|
|
+console.log(await goodById('5dc458985df9d670df48cc47'), 'goodById');
|