|
@@ -2,13 +2,12 @@
|
|
|
|
|
|
const getGQL = (url) => async (query, variables) => {
|
|
|
try {
|
|
|
- const token = localStorage.getItem('token');
|
|
|
- const Authorization = token ? `Bearer ${token}` : '';
|
|
|
+ const token = localStorage.token;
|
|
|
const res = await fetch(url, {
|
|
|
method: 'POST',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
|
- Authorization,
|
|
|
+ Authorization: token ? 'Bearer ' + token : '',
|
|
|
},
|
|
|
body: JSON.stringify({ query, variables }),
|
|
|
});
|
|
@@ -30,7 +29,7 @@ query log($login:String, $password:String){
|
|
|
{ login, password }
|
|
|
);
|
|
|
const token = data.login;
|
|
|
- if (token) localStorage.setItem('token', token);
|
|
|
+ if (token) localStorage.token = token;
|
|
|
return token;
|
|
|
} catch (e) {
|
|
|
console.error(e);
|
|
@@ -76,9 +75,10 @@ const goodsGQL = async () => {
|
|
|
const { data } = await gql(
|
|
|
`query allGoods{
|
|
|
GoodFind(query:"[{}]"){
|
|
|
- _id,
|
|
|
+ _id
|
|
|
name,
|
|
|
- createdAt
|
|
|
+ createdAt,
|
|
|
+ price,
|
|
|
}
|
|
|
}`,
|
|
|
{}
|
|
@@ -91,16 +91,18 @@ const goodsGQL = async () => {
|
|
|
|
|
|
const categoryById = async (_id) => {
|
|
|
try {
|
|
|
- const data = await gql(
|
|
|
+ const { data } = await gql(
|
|
|
`query categoryById($id:String){
|
|
|
CategoryFindOne(query:$id){
|
|
|
- name,
|
|
|
- _id
|
|
|
- }
|
|
|
+ _id,name,
|
|
|
+ goods {
|
|
|
+ _id,createdAt,name
|
|
|
+ }
|
|
|
+ },
|
|
|
}`,
|
|
|
{ id: JSON.stringify([{ _id }]) }
|
|
|
);
|
|
|
- return data.data.CategoryFindOne;
|
|
|
+ return data.CategoryFindOne;
|
|
|
} catch (e) {
|
|
|
console.error(e);
|
|
|
}
|
|
@@ -108,16 +110,18 @@ const categoryById = async (_id) => {
|
|
|
|
|
|
const goodById = async (_id) => {
|
|
|
try {
|
|
|
- const data = await gql(
|
|
|
- `query goodById($id:String){
|
|
|
- GoodFindOne(query:$id){
|
|
|
- name,
|
|
|
- _id
|
|
|
- }
|
|
|
- }`,
|
|
|
+ const { data } = await gql(
|
|
|
+ `query findById($id:String){
|
|
|
+ GoodFindOne (query:$id){
|
|
|
+ _id name price description images {
|
|
|
+ url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }`,
|
|
|
{ id: JSON.stringify([{ _id }]) }
|
|
|
);
|
|
|
- return data.data.GoodFindOne;
|
|
|
+
|
|
|
+ return data.GoodFindOne;
|
|
|
} catch (e) {
|
|
|
console.error(e);
|
|
|
}
|
|
@@ -127,5 +131,5 @@ 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('5dc458985df9d670df48cc47'), 'categoryById');
|
|
|
+console.log(await categoryById('5dc4b2553f23b553bf354100'), 'categoryById');
|
|
|
console.log(await goodById('61afa803c750c12ba6ba444a'), 'goodById');
|