GraphQL.js 554 B

123456789101112131415
  1. export const getGQL = (url) => (query, variables) =>
  2. fetch(url, {
  3. method: "POST",
  4. headers: {
  5. "Content-Type": "application/json",
  6. ...(localStorage.authToken ? { Authorization: "Bearer " + localStorage.authToken } : {}),
  7. },
  8. body: JSON.stringify({ query, variables }),
  9. })
  10. .then((res) => res.json())
  11. .then((data) => {
  12. if (data.errors) {
  13. throw new Error(JSON.stringify(data.errors));
  14. } else return Object.values(data.data)[0];
  15. });