|
@@ -0,0 +1,103 @@
|
|
|
+
|
|
|
+const getGQL = url =>
|
|
|
+ (query, variables) => fetch(url, {
|
|
|
+
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ body: JSON.stringify({ query, variables })
|
|
|
+
|
|
|
+
|
|
|
+ }).then(res => res.json()).then(data => {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return data
|
|
|
+ });
|
|
|
+
|
|
|
+const gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql');
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+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}
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+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}
|
|
|
+};
|
|
|
+
|
|
|
+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"));
|
|
|
+
|
|
|
+
|
|
|
+
|