Skip to content

Commit d9cb9ca

Browse files
author
Matthew Valancy
committed
Fix GraphQL client to work with external connections
- Update Apollo client to dynamically use same hostname with port 4127 - Remove hardcoded localhost URLs that break external access - Support both local and remote access patterns
1 parent 63ad776 commit d9cb9ca

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

packages/web/src/lib/apollo.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,32 @@ import { getMainDefinition } from '@apollo/client/utilities';
33
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
44
import { createClient } from 'graphql-ws';
55

6+
// Dynamically construct GraphQL URLs based on current host
7+
const getGraphQLUrl = () => {
8+
if (import.meta.env.VITE_GRAPHQL_URL) {
9+
return import.meta.env.VITE_GRAPHQL_URL;
10+
}
11+
// Use same hostname but port 4127 for GraphQL
12+
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
13+
return `${protocol}//${window.location.hostname}:4127/graphql`;
14+
};
15+
16+
const getWebSocketUrl = () => {
17+
if (import.meta.env.VITE_GRAPHQL_WS_URL) {
18+
return import.meta.env.VITE_GRAPHQL_WS_URL;
19+
}
20+
// Use same hostname but port 4127 for WebSocket
21+
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
22+
return `${protocol}//${window.location.hostname}:4127/graphql`;
23+
};
24+
625
const httpLink = createHttpLink({
7-
uri: import.meta.env.VITE_GRAPHQL_URL || '/graphql',
26+
uri: getGraphQLUrl(),
827
});
928

1029
const wsLink = new GraphQLWsLink(
1130
createClient({
12-
url: import.meta.env.VITE_GRAPHQL_WS_URL || `ws://${window.location.host}/graphql`,
31+
url: getWebSocketUrl(),
1332
})
1433
);
1534

0 commit comments

Comments
 (0)