123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- const getGQL = url => {
- return (query, variables) => {
- return fetch(url, {
- method: 'POST',
- headers: {
- "content-type": "application/json",
- ...(localStorage.authToken ? { Authorization: "Bearer " + localStorage.authToken } : {})
- },
- body: JSON.stringify({ query, variables }),
- }).then(res => res.json())
- }
- }
- let gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql")
- let getId = async (type, name) => {
- let t = type.toLowerCase();
- let query;
- if (t === "category") {
- query = `query fndCtgId($n: String) {
- CategoryFind(query: $n){
- _id
- }
- }`
- } else if (t === "good") {
- query = `query fndCtgId($n: String) {
- GoodFind(query: $n){
- _id
- }
- }`
- }
- let qVariables = {
- "n": JSON.stringify([{ "name": name }])
- }
- let res = await gql(query, qVariables)
- return res
- }
- let categoryById = async (id) => {
- let query = `query fndcategory($id: String) {
- CategoryFind(query: $id){
- name goods{
- name price images {
- url
- }
- }
- }
- }`
- let qVariables = {
- "id": JSON.stringify([{ "_id": id }])
- }
- let res = await gql(query, qVariables)
- console.log(res)
- return res
- }
- let goodById = async (id) => {
- let query = `query fndgood($id: String) {
- GoodFind(query: $id){
- name price images {
- url
- }
- }
- }`
- let qVariables = {
- "id": JSON.stringify([{ "_id": id }])
- }
- let res = await gql(query, qVariables)
- return res
- }
- let addProduct = async ({ name, price, description, imgSrc, categories }) => {
- let query = `mutation addGood($product: GoodInput) {
- GoodUpsert(good: $product){
- _id
- }
- }`
- let qVariables = {
- "product": JSON.stringify([{ "name": name, "price": price, "description": description, "images": [{ "url": imgSrc }], "categories": [{ "_id": categories }] }])
- }
- let res = await gql(query, qVariables)
- return res
- }
- let deleteProdact = async (id) => {
- let query = `mutation goodDel($id: GoodInput) {
- GoodDelete(good: $id)
- }`
- let qVariables = {
- "id": JSON.stringify([{ "_id": id }])
- }
- let res = await gql(query, qVariables)
- return res
- }
|