GraphQL + Fetch API

Execute GraphQL queries and mutations using the fetch API.

Notes

const query = `
query GetCartById($id: ID!) {
cart(id: $id) {
totalItems
subTotal {
amount
formatted
}
items {
id
name
quantity
unitTotal {
formatted
}
}
}
}
`;
const variables = {
id: "ck5r8d5b500003f5o2aif0v2b",
};
const body = JSON.stringify({ query, variables });
fetch("https://api.cartql.com", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body,
})
.then((res) => res.json())
.then(({ data }) => console.log(data));
const axios = require("axios");
const request = axios({
url: endpoint,
method: "POST",
headers: {
"Content-Type": "application/json",
},
data: body,
});
request.then(({ data: { data } }) => console.log(data));
Jamie Barton

Published on 4 Oct 2021 by Jamie Barton