getGQL.js 745 B

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