export const getGQL = url =>
    async (query, variables = {}) => {
        let obj = await fetch(url, {
            method: 'POST',
            headers: {
                "Content-Type": "application/json",
                Authorization: localStorage.authToken ? 'Bearer ' + localStorage.authToken : {},
            },
            body: JSON.stringify({ query, variables })
        })
        let a = await obj.json()
        if (!a.data && a.errors)
            throw new Error(JSON.stringify(a.errors))
        return a.data[Object.keys(a.data)[0]]
    }

export const backURL = 'http://player.asmer.fs.a-level.com.ua'

export const gql = getGQL(backURL + '/graphql');


export const queries = {
    "/user/:_id": match => ({name: 'playlistFindByUser',
        query: `query playlistFindByUser ($q: String) {
    PlaylistFind(query: $q) {
        _id name owner {login}
    }
}`, variables: { q: JSON.stringify([{ ___owner: match.params._id }]) } }),

    "/myPlaylist/:_id": match => ({name: 'playlistById',
        query: `query playlistById($q: String){
    PlaylistFindOne(query: $q){
        _id name owner {login} tracks {_id url originalFileName}
    }
}`, variables:{ q: JSON.stringify([{ _id: match.params._id }]) } }),


    "/userPlaylist/:_id": match => ({name: 'playlistById',
        query: `query playlistById($q: String){
    PlaylistFindOne(query: $q){
        _id name owner {login} tracks {_id url originalFileName}
    }
}`, variables:{ q: JSON.stringify([{ _id: match.params._id }]) } }),

}
