getGQL.js 607 B

1234567891011121314151617181920212223
  1. import backendURL from './backendUrl'
  2. export const getGQL = (url) => (query, variables) =>
  3. fetch(url, {
  4. method: 'POST',
  5. headers: {
  6. 'Content-Type': '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 {
  18. throw new Error(JSON.stringify(data.errors))
  19. }
  20. })
  21. export const gql = getGQL(backendURL+'/graphql')