
const backURL = "http://shop-roles.node.ed.asmer.org.ua/graphql";

let gql = (url, query, variables = {}) =>
    fetch(url, {
        method: "POST",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
        },
        body: JSON.stringify({ query, variables }),
    }).then((res) => res.json());

const categoryFind = () =>
    gql(
        backURL,
        `
            query catz{
                CategoryFind(query:"[{}]") {
                    name
                }
            }
            `
    );

const registerUser = (login, password) =>
    gql(
        backURL,
        `
            mutation register($login:String, $password:String){
                UserUpsert(user: {login:$login, password:$password}){
                    _id login createdAt
                }
            }
        `,
        { login, password }
    );

const login = (login, password) =>
    gql(
        backURL,
        `
            query login($login:String, $password:String){
                login(login:$login, password:$password)
            }
        `,
        { login, password }
    );

const categories = () =>
    gql(
        backURL,
        `
                query categoryFind{
                    CategoryFind(query:"[{}]"){
                    _id name goods {
                        _id
                        createdAt
                        name
                        description
                        price
                    }
                }
            }`
    );


(async () => {
    console.log("all category:", await categoryFind());
    console.log(
        "register user:",
        await registerUser("antonPyvovarov", "qwerty12345")
    );
    console.log(
        "user login:",
        await login("antonPyvovarov", "qwerty12345")
    );
    console.log(
        "category by id with products:",
        await categories()
    );
    console.log(
        "product search by id:",
        await productSearchByIdImgDiscr("62c9472cb74e1f5f2ec1a0d3")
    );
})();



