diff --git a/client-next/src/hooks/useTripQuery.ts b/client-next/src/hooks/useTripQuery.ts index e96a04d7cc3..b5c14d406f8 100644 --- a/client-next/src/hooks/useTripQuery.ts +++ b/client-next/src/hooks/useTripQuery.ts @@ -5,6 +5,17 @@ import { QueryType, TripQueryVariables } from '../gql/graphql.ts'; const endpoint = import.meta.env.VITE_API_URL; +const getApiUrl = () => { + try { + // the URL constructor will throw exception if endpoint is not a valid URL, + // e.g. if it is a relative path + new URL(endpoint); + return endpoint; + } catch (e) { + return `${window.location.origin}${endpoint}`; + } +}; + /** General purpose trip query document for debugging trip searches TODO: should live in a separate file, and split into fragments for readability @@ -96,9 +107,9 @@ export const useTripQuery: TripQueryHook = (variables) => { if (variables) { setLoading(true); if (pageCursor) { - setData((await request(endpoint, query, { ...variables, pageCursor })) as QueryType); + setData((await request(getApiUrl(), query, { ...variables, pageCursor })) as QueryType); } else { - setData((await request(endpoint, query, variables)) as QueryType); + setData((await request(getApiUrl(), query, variables)) as QueryType); } setLoading(false); } else {