|
@@ -0,0 +1,103 @@
|
|
|
+// ДЗ: допилить:
|
|
|
+const getGQL = url =>
|
|
|
+ (query, variables) => fetch(url, {
|
|
|
+ //метод
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ //заголовок content-type
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ body: JSON.stringify({ query, variables })
|
|
|
+ //body с ключами query и variables
|
|
|
+
|
|
|
+ }).then(res => res.json()).then(data => {
|
|
|
+ //расковырять data, если все ок - отдать data.login или data.
|
|
|
+ // CategoryFindOne, или шо там еще
|
|
|
+ //если есть errors и нет data, то выбросить исключение и тем самым зареджектить промис
|
|
|
+ return data
|
|
|
+ });
|
|
|
+
|
|
|
+const gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql');
|
|
|
+// console.log((await gql(`
|
|
|
+// query NameForMe1($login:String, $password:String){
|
|
|
+// login(login:$login, password:$password)
|
|
|
+// }
|
|
|
+// `, { login: 'tst', password: '123' })).data.login)
|
|
|
+
|
|
|
+// написать тестовых запросов:
|
|
|
+// логин, регистрация, категории все, категория по id, товар по id
|
|
|
+// Регистрация
|
|
|
+async function gqlRegistration(login, password) {
|
|
|
+ try {
|
|
|
+ let result = await gql(`mutation register ($login:String, $password:String)
|
|
|
+ {UserUpsert(user:{login:$login, password:$password, nick:$login}) {_id}}`,
|
|
|
+ { "login": login, "password": password });
|
|
|
+ return (result.errors?result.errors[0].message:result.data.UserUpsert._id);
|
|
|
+ }
|
|
|
+ catch (e) { e}
|
|
|
+};
|
|
|
+
|
|
|
+ // Авторизация
|
|
|
+async function gqlLogin(login, password) {
|
|
|
+ try {
|
|
|
+ let result = await gql(`query log ($login:String, $password:String)
|
|
|
+ { login(login:$login, password:$password)}`,
|
|
|
+ { "login": login, "password": password });
|
|
|
+
|
|
|
+ return (result.data.login?result.data.login:'Пользователь не найден')
|
|
|
+ }
|
|
|
+ catch(e) {e}
|
|
|
+};
|
|
|
+// Все категории
|
|
|
+async function gqlCats() {
|
|
|
+ try {
|
|
|
+ let result = await gql(`
|
|
|
+ query cats($q:String){
|
|
|
+ CategoryFind(query:$q){_id name parent {
|
|
|
+ _id
|
|
|
+ createdAt
|
|
|
+ name
|
|
|
+ }}
|
|
|
+ }`, {"q": "[{}]"});
|
|
|
+ return (!result.errors?result.data:result.errors[0].message)
|
|
|
+ }
|
|
|
+ catch(e) {e}
|
|
|
+};
|
|
|
+
|
|
|
+//Категория по id
|
|
|
+async function gqlCatById(catId) {
|
|
|
+ try {
|
|
|
+ const catIdQ = `[{"_id":"${catId}"}]`;
|
|
|
+ let result = await gql(`query catById($catId:String){
|
|
|
+ CategoryFindOne(query:$catId)
|
|
|
+ {_id name parent{name} subCategories{name}
|
|
|
+ }}`,
|
|
|
+ { "catId": catIdQ });
|
|
|
+ return (!result.errors?result.data:result.errors[0].message)
|
|
|
+ }
|
|
|
+ catch(e) {e}
|
|
|
+};
|
|
|
+//Товар по id
|
|
|
+async function gqlGoodById(goodId) {
|
|
|
+ try {
|
|
|
+ const goodIdQ = `[{"_id":"${goodId}"}]`;
|
|
|
+ let result = await gql(`query goodById($goodId:String){
|
|
|
+ GoodFindOne(query:$goodId)
|
|
|
+ {_id name price owner{nick}
|
|
|
+ }}`,
|
|
|
+ { "goodId": goodIdQ });
|
|
|
+ return (!result.errors?result.data:result.errors[0].message)
|
|
|
+ }
|
|
|
+ catch(e) {e}
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+console.log(gqlLogin("Olyatest3", "000"));
|
|
|
+console.log(gqlLogin("OlyaBrekh", "arriba"));
|
|
|
+console.log(gqlRegistration("OlyaBrekh", "arriba"));
|
|
|
+console.log(gqlCats());
|
|
|
+console.log(gqlCatById("5dc4b2553f23b553bf3540fc"));
|
|
|
+console.log(gqlGoodById("5dc45c3d5df9d670df48cc4a"));
|
|
|
+
|
|
|
+
|
|
|
+
|