Experimenting using GraphQL in favor of REST.

This commit is contained in:
2019-12-15 05:42:51 +01:00
parent 36c5fa3b52
commit 7ba716cd6f
21 changed files with 1021 additions and 113 deletions

View File

@@ -21,7 +21,7 @@ async function get(endpoint, query = {}) {
async function post(endpoint, data) {
const res = await fetch(`${config.api.url}${endpoint}`, {
method: 'GET',
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
@@ -39,4 +39,34 @@ async function post(endpoint, data) {
throw new Error(errorMsg);
}
export { get, post };
async function graphql(operationName, query, variables = null) {
const res = await fetch('/graphql', {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
credentials: 'same-origin',
body: JSON.stringify({
operationName,
query,
variables,
}),
});
if (res.ok) {
const { data } = await res.json();
return data;
}
const errorMsg = await res.text();
throw new Error(errorMsg);
}
export {
get,
post,
graphql,
};