Skip to content

Commit

Permalink
Merge pull request #5899 from entur/fix-graphql-request-breaking-chan…
Browse files Browse the repository at this point in the history
…ge-in-url

Fix debug client after breaking change in dependency graphql-request
  • Loading branch information
leonardehrenfried authored Jun 11, 2024
2 parents 6916e66 + 0177051 commit 1a720cc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 2 additions & 3 deletions client-next/src/hooks/useServerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { useEffect, useState } from 'react';
import { graphql } from '../gql';
import { request } from 'graphql-request'; // eslint-disable-line import/no-unresolved
import { QueryType } from '../gql/graphql.ts';

const endpoint = import.meta.env.VITE_API_URL;
import { getApiUrl } from '../util/getApiUrl.ts';

const query = graphql(`
query serverInfo {
Expand All @@ -22,7 +21,7 @@ export const useServerInfo = () => {
const [data, setData] = useState<QueryType | null>(null);
useEffect(() => {
const fetchData = async () => {
setData((await request(endpoint, query)) as QueryType);
setData((await request(getApiUrl(), query)) as QueryType);
};
fetchData();
}, []);
Expand Down
7 changes: 3 additions & 4 deletions client-next/src/hooks/useTripQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { useCallback, useEffect, useState } from 'react';
import { graphql } from '../gql';
import { request } from 'graphql-request'; // eslint-disable-line import/no-unresolved
import { QueryType, TripQueryVariables } from '../gql/graphql.ts';

const endpoint = import.meta.env.VITE_API_URL;
import { getApiUrl } from '../util/getApiUrl.ts';

/**
General purpose trip query document for debugging trip searches
Expand Down Expand Up @@ -96,9 +95,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 {
Expand Down
12 changes: 12 additions & 0 deletions client-next/src/util/getApiUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const endpoint = import.meta.env.VITE_API_URL;

export 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}`;
}
};

0 comments on commit 1a720cc

Please sign in to comment.