123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- const getGQL = (url) => async (query, variables) => {
- try {
- const token = localStorage.token ? localStorage.token : '';
- const res = await fetch(url, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: 'Bearer ' + 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
- }
- }`,
- { 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,
- };
|