Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix debug client after breaking change in dependency graphql-request #5899

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this line do anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it throws an exception if endpoint is not a valid URL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 oh ok, reading the comments helps

return endpoint;
} catch (e) {
return `${window.location.origin}${endpoint}`;
}
};
Loading