|
@@ -1,14 +1,11 @@
|
|
|
-'use strict';
|
|
|
-
|
|
|
const getGQL = (url) => async (query, variables) => {
|
|
|
try {
|
|
|
- const token = localStorage.token;
|
|
|
- console.log(token);
|
|
|
+ const token = localStorage.token ? localStorage.token : '';
|
|
|
const res = await fetch(url, {
|
|
|
method: 'POST',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
|
- Authorization: 'Bearer ' + token ? token : '',
|
|
|
+ Authorization: 'Bearer ' + token,
|
|
|
},
|
|
|
body: JSON.stringify({ query, variables }),
|
|
|
});
|
|
@@ -80,6 +77,9 @@ const goodsGQL = async () => {
|
|
|
name,
|
|
|
createdAt,
|
|
|
price,
|
|
|
+ images {
|
|
|
+ url
|
|
|
+ }
|
|
|
}
|
|
|
}`,
|
|
|
{}
|
|
@@ -95,7 +95,7 @@ const categoryById = async (_id) => {
|
|
|
const { data } = await gql(
|
|
|
`query categoryById($id:String){
|
|
|
CategoryFindOne(query:$id){
|
|
|
- _id,name,
|
|
|
+ _id,name,createdAt,
|
|
|
goods {
|
|
|
_id,createdAt,name
|
|
|
}
|
|
@@ -114,7 +114,7 @@ const goodById = async (_id) => {
|
|
|
const { data } = await gql(
|
|
|
`query findById($id:String){
|
|
|
GoodFindOne (query:$id){
|
|
|
- _id name price description images {
|
|
|
+ _id createdAt name price description images {
|
|
|
url
|
|
|
}
|
|
|
}
|
|
@@ -128,13 +128,14 @@ const goodById = async (_id) => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-const getOrders = async () => {
|
|
|
+const ordersGQL = async () => {
|
|
|
try {
|
|
|
const { data } = await gql(
|
|
|
`query orders {
|
|
|
OrderFind(query:"[{}]")
|
|
|
{
|
|
|
- _id
|
|
|
+ _id
|
|
|
+ createdAt
|
|
|
total
|
|
|
orderGoods {
|
|
|
price
|
|
@@ -148,14 +149,29 @@ const getOrders = async () => {
|
|
|
}`,
|
|
|
{}
|
|
|
);
|
|
|
-
|
|
|
return data.OrderFind;
|
|
|
} catch (e) {
|
|
|
console.error(e);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-const makeOrder = async (id) => {
|
|
|
+const orderById = async (_id) => {
|
|
|
+ try {
|
|
|
+ const { data } = await gql(
|
|
|
+ `query findById($id:String){
|
|
|
+ OrderFindOne (query:$id){
|
|
|
+ _id createdAt
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ { 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){
|
|
@@ -164,7 +180,7 @@ const makeOrder = async (id) => {
|
|
|
{count:1,good:{_id:$id}}
|
|
|
]
|
|
|
}){
|
|
|
- _id,total
|
|
|
+ _id,createdAt,total
|
|
|
}
|
|
|
}`,
|
|
|
{ id }
|
|
@@ -176,11 +192,14 @@ const makeOrder = async (id) => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-console.log(await loginGQL('test2021121', 'test2021121'), 'loginGQL');
|
|
|
-console.log(await registerGQL('1234332121', '123'), 'registerGQL');
|
|
|
-console.log(await categoriesGQL(), 'categoriesGQL');
|
|
|
-console.log(await goodsGQL(), 'goodsGQL');
|
|
|
-console.log(await categoryById('5dc4b2553f23b553bf354100'), 'categoryById');
|
|
|
-console.log(await goodById('61afa803c750c12ba6ba444a'), 'goodById');
|
|
|
-console.log(await makeOrder('5dc886550e36db246e3049c0', 2), 'makeOrder');
|
|
|
-console.log(await getOrders(), 'getOrders');
|
|
|
+export {
|
|
|
+ loginGQL,
|
|
|
+ registerGQL,
|
|
|
+ categoriesGQL,
|
|
|
+ goodsGQL,
|
|
|
+ categoryById,
|
|
|
+ goodById,
|
|
|
+ ordersGQL,
|
|
|
+ orderById,
|
|
|
+ makeOrderById,
|
|
|
+};
|