|
@@ -0,0 +1,208 @@
|
|
|
+const getGQL = url => async (query, variables) => {
|
|
|
+ try {
|
|
|
+ const token = localStorage.token;
|
|
|
+ const res = await fetch(url, {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ Authorization: 'Bearer ' + token ? token : '',
|
|
|
+ },
|
|
|
+ body: JSON.stringify({ query, variables }),
|
|
|
+ });
|
|
|
+ return res.json();
|
|
|
+ } catch (e) {
|
|
|
+ return 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.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.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const categoriesGQL = async () => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `query allCategories{
|
|
|
+ CategoryFind(query:"[{}]"){
|
|
|
+ _id,
|
|
|
+ name,
|
|
|
+ createdAt
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ {},
|
|
|
+ );
|
|
|
+ return data.CategoryFind;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const goodsGQL = async () => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `query allGoods{
|
|
|
+ GoodFind(query:"[{}]"){
|
|
|
+ _id
|
|
|
+ name,
|
|
|
+ createdAt,
|
|
|
+ price,
|
|
|
+ images {
|
|
|
+ url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ {},
|
|
|
+ );
|
|
|
+ return data.GoodFind;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const categoryById = async _id => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `query categoryById($id:String){
|
|
|
+ CategoryFindOne(query:$id){
|
|
|
+ _id,name,createdAt,
|
|
|
+ goods {
|
|
|
+ _id,createdAt,name
|
|
|
+ }
|
|
|
+ },
|
|
|
+ }`,
|
|
|
+ { id: JSON.stringify([{ _id }]) },
|
|
|
+ );
|
|
|
+ return data.CategoryFindOne;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const goodById = async _id => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `query findById($id:String){
|
|
|
+ GoodFindOne (query:$id){
|
|
|
+ _id createdAt name price description images {
|
|
|
+ url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ { id: JSON.stringify([{ _id }]) },
|
|
|
+ );
|
|
|
+
|
|
|
+ return data.GoodFindOne;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const ordersGQL = async () => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `query orders {
|
|
|
+ OrderFind(query:"[{}]")
|
|
|
+ {
|
|
|
+ _id
|
|
|
+ createdAt
|
|
|
+ total
|
|
|
+ orderGoods {
|
|
|
+ price
|
|
|
+ count
|
|
|
+ total
|
|
|
+ good {
|
|
|
+ name
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}`,
|
|
|
+ {},
|
|
|
+ );
|
|
|
+ return data.OrderFind;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const orderById = async _id => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `query findById($id:String){
|
|
|
+ OrderFindOne (query:$id){
|
|
|
+ _id createdAt name price description images {
|
|
|
+ url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ { id: JSON.stringify([{ _id }]) },
|
|
|
+ );
|
|
|
+
|
|
|
+ return data.OrderFindOne;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const makeOrderById = async id => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ ` mutation makeOrder($id:ID){
|
|
|
+ OrderUpsert(order:{
|
|
|
+ orderGoods:[
|
|
|
+ {count:1,good:{_id:$id}}
|
|
|
+ ]
|
|
|
+ }){
|
|
|
+ _id,createdAt,total
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ { id },
|
|
|
+ );
|
|
|
+
|
|
|
+ return data.OrderUpsert;
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+export {
|
|
|
+ loginGQL,
|
|
|
+ registerGQL,
|
|
|
+ categoriesGQL,
|
|
|
+ goodsGQL,
|
|
|
+ categoryById,
|
|
|
+ goodById,
|
|
|
+ ordersGQL,
|
|
|
+ orderById,
|
|
|
+ makeOrderById,
|
|
|
+};
|