|
@@ -0,0 +1,79 @@
|
|
|
+
|
|
|
+const getGQL = url =>
|
|
|
+ (query, variables) => fetch(url, {
|
|
|
+ method: 'POST',
|
|
|
+ headers: { //заголовок content-type
|
|
|
+ "Content-Type": "application/json",
|
|
|
+ ...(localStorage.authToken ? {"Authorization": "Bearer " + localStorage.authToken} : {})
|
|
|
+ },
|
|
|
+ body: JSON.stringify({query, variables}) //body с ключами query и variables
|
|
|
+ }).then(res => res.json())
|
|
|
+ .then(data => {
|
|
|
+ if (data.data){
|
|
|
+ console.log(data);
|
|
|
+ return data.data;
|
|
|
+ }
|
|
|
+ else throw new Error(JSON.stringify(data.errors))
|
|
|
+ })
|
|
|
+
|
|
|
+const backendURL = 'http://shop-roles.asmer.fs.a-level.com.ua'
|
|
|
+
|
|
|
+const gql = getGQL(backendURL + '/graphql')
|
|
|
+
|
|
|
+
|
|
|
+function register(login, password) {
|
|
|
+ return gql(
|
|
|
+ `mutation reg($login:String, $password:String) {UserUpsert(user: {login:$login, password: $password}) {_id login} }`,
|
|
|
+ { login: login, password: password }
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+register("Natasha", "12345").then(res=>console.log(res));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+const login = (login, password) => {
|
|
|
+ return gql(` query log ($login:String, $password:String){
|
|
|
+ login(login:$login, password: $password) }`,
|
|
|
+ {login:login, password:password})
|
|
|
+}
|
|
|
+login("Natasha","12345").then(res=>console.log(res))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+const rootCats= () =>
|
|
|
+ gql(`query {
|
|
|
+ CategoryFind(query: "[{\\"parent\\":null}]"){
|
|
|
+ _id name
|
|
|
+ }
|
|
|
+}`)
|
|
|
+rootCats().then(res=>console.log(res))
|
|
|
+
|
|
|
+
|
|
|
+const findCatById=(_id)=> gql(`query catById($q: String){
|
|
|
+ CategoryFindOne(query: $q){
|
|
|
+ _id name goods {
|
|
|
+ _id name price images {
|
|
|
+ url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}`, {q: JSON.stringify([{_id}])} )
|
|
|
+
|
|
|
+findCatById("61715f99ef4e1b3e3b67703e").then(res=>console.log(res))
|
|
|
+
|
|
|
+
|
|
|
+const findGoodById=(_id)=> gql(`
|
|
|
+ query goodById($q: String){
|
|
|
+ GoodFindOne(query:$q){
|
|
|
+ _id
|
|
|
+ name
|
|
|
+ price
|
|
|
+ description
|
|
|
+ categories{_id name}
|
|
|
+
|
|
|
+ }
|
|
|
+} `, {q: JSON.stringify([{_id}])})
|
|
|
+
|
|
|
+findGoodById("61782338ef4e1b3e3b677079").then(res=>console.log(res))
|
|
|
+
|