123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- const getGQL = url => {
- return function(query, variables) {
- return fetch(url,
- {
- method: "POST",
- headers: {"Content-Type": "application/json"},
- body: JSON.stringify({query, variables})
- }).then(resp => resp.json()).then(data => {
- if ("errors" in data) {
- throw new Error('ашипка, угадывай што не так')
- }
- else {
- return data.data[Object.keys(variables)[0]]
- }
- })
- }
- }
- let gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')
- //запросики
- //categories
- async function getCategoriesCount() {
- let result = await gql(`
- query ($query:String){
- CategoryCount(query: $query)
- }`, {CategoryCount: '', query: "[{}]"})
- return result
- }
- async function findAllCategories() {
- let result = await gql(`
- query ($query:String){
- CategoryFind(query: $query) {
- _id
- name
- image {
- _id text url
- }
- goods {
- _id name description price images {
- _id text url
- }
- }
- }
- }`, {CategoryFind: '', query: "[{}]"})
- return result
- }
- async function findCategoryById(categoryId) {
- let id = `[{"_id":"${categoryId}"}]`
- let result = await gql(`
- query ($id:String){
- CategoryFindOne(query: $id){
- name
- image {
- _id text url
- }
- goods {
- _id name description price images {
- _id text url
- }
- }
- }
- }`, { CategoryFindOne: '', id })
- return result
- }
- async function findSubCategories(categoryId) {
- let id = `[{"_id":"${categoryId}"}]`
- let result = await gql(`
- query($id:String){
- CategoryFindOne(query: $id){
- subCategories {
- _id
- name
- image {
- _id text url originalFileName
- }
- goods {
- _id name description price images {
- _id text url
- }
- }
- }
- }
- }`, { CategoryFindOne: '', id })
- return result
- }
- //goods
- async function getGoodsCount() {
- let result = await gql(`
- query ($query:String){
- GoodCount(query: $query)
- }`, {GoodCount: '', query: "[{}]"})
- return result
- }
- async function findAllGoods() {
- let result = await gql(`
- query ($query:String){
- GoodFind(query: $query) {
- _id name description price images {
- _id text url
- }
- categories {
- _id name
- }
- }
- }`, {GoodFind: '', query: "[{}]"})
- return result
- }
- async function findGoodById(goodId) {
- let id = `[{"_id":"${goodId}"}]`
- let result = await gql(`
- query good($id:String){
- GoodFindOne(query: $id) {
- name description price images {
- _id text url
- }
- categories {
- _id name
- }
- }
- }`, { GoodFindOne: '', id })
- return result
- }
- //users
- async function getUsersCount() {
- let result = await gql(`
- query ($query:String){
- UserCount(query: $query)
- }`, {UserCount: '', query: "[{}]"})
- return result
- }
- async function findAllUsers() {
- let result = await gql(`
- query ($query:String){
- UserFind(query: $query){
- _id createdAt login nick acl avatar {
- _id text url
- }
- }
- }`, {UserFind: '', query: "[{}]"})
- return result
- }
- async function findUserById(userId) {
- let id = `[{"_id":"${userId}"}]`
- let result = await gql(`
- query ($id:String){
- UserFindOne(query: $id){
- _id createdAt login nick acl avatar {
- _id text url
- }
- }
- }`, { UserFindOne: '', id })
- return result
- }
- //images
- async function getImagesCount() {
- let result = await gql(`
- query ($query:String){
- ImageCount(query: $query)
- }`, {ImageCount: '', query: "[{}]"})
- return result
- }
- async function findAllImages() {
- let result = await gql(`
- query ($query:String){
- ImageFind(query: $query) {
- _id createdAt text url originalFileName
- }
- }`, {ImageFind: '', query: "[{}]"})
- return result
- }
- //orders
- async function findAllOrders() {
- let result = await gql(`
- query ($query:String) {
- OrderFind(query: $query){
- _id
- createdAt
- total
- orderGoods {
- _id count price good {
- _id name
- }
- }
- owner {
- _id login acl
- }
- }
- }`, {OrderFind: '', query: "[{}]"})
- return result
- }
- async function findOrderById(orderId) {
- let id = `[{"_id":"${orderId}"}]`
- let result = await gql(`
- query ($id:String){
- OrderFindOne(query: $id){
- _id
- createdAt
- total
- orderGoods {
- _id count price good {
- _id name
- }
- }
- owner {
- _id login acl
- }
- }
- }`, { OrderFindOne: '', id })
- return result
- }
- //reg
- async function registerUser(login, password) {
- let result = await gql(`
- mutation ($login:String, $password:String) {
- UserUpsert(user:{
- login:$login, password:$password
- }) {
- _id
- login
- }
- }`, {UserUpsert: '', login, password})
- return result
- }
- //всё, хватит..........
|