GraphQL + Fetch API

Execute GraphQL queries and mutations using the fetch API.

Notes

1const query = `
2 query GetCartById($id: ID!) {
3 cart(id: $id) {
4 totalItems
5 subTotal {
6 amount
7 formatted
8 }
9 items {
10 id
11 name
12 quantity
13 unitTotal {
14 formatted
15 }
16 }
17 }
18 }
19`;
20
21const variables = {
22 id: "ck5r8d5b500003f5o2aif0v2b",
23};
24
25const body = JSON.stringify({ query, variables });
26
27fetch("https://api.cartql.com", {
28 method: "POST",
29 headers: {
30 "Content-Type": "application/json",
31 },
32 body,
33})
34 .then((res) => res.json())
35 .then(({ data }) => console.log(data));
36
1const axios = require("axios");
2
3const request = axios({
4 url: endpoint,
5 method: "POST",
6 headers: {
7 "Content-Type": "application/json",
8 },
9 data: body,
10});
11
12request.then(({ data: { data } }) => console.log(data));
13