getGQL.js 589 B

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