refactor(web): utilize REACT_APP_BACKEND_URL for GQL and REST API URLs

This commit is contained in:
Ali BARIN
2024-03-01 12:37:17 +00:00
parent 917de46c45
commit 73bd90c555
5 changed files with 34 additions and 6 deletions

View File

@@ -28,7 +28,7 @@ cd packages/web
rm -rf .env rm -rf .env
echo " echo "
PORT=$WEB_PORT PORT=$WEB_PORT
REACT_APP_GRAPHQL_URL=http://localhost:$BACKEND_PORT/graphql REACT_APP_BACKEND_URL=http://localhost:$BACKEND_PORT
" >> .env " >> .env
cd $CURRENT_DIR cd $CURRENT_DIR

View File

@@ -1,4 +1,4 @@
PORT=3001 PORT=3001
REACT_APP_GRAPHQL_URL=http://localhost:3000/graphql REACT_APP_BACKEND_URL=http://localhost:3000
# HTTPS=true # HTTPS=true
REACT_APP_BASE_URL=http://localhost:3001 REACT_APP_BASE_URL=http://localhost:3001

View File

@@ -48,7 +48,7 @@
"test": "react-scripts test", "test": "react-scripts test",
"eject": "react-scripts eject", "eject": "react-scripts eject",
"lint": "eslint .", "lint": "eslint .",
"prepack": "REACT_APP_GRAPHQL_URL=/graphql REACT_APP_REST_API_URL=/api yarn build" "prepack": "yarn build"
}, },
"files": [ "files": [
"/build" "/build"

View File

@@ -1,8 +1,25 @@
const backendUrl = process.env.REACT_APP_BACKEND_URL;
const computeUrl = (url, backendUrl) => {
/**
* In case `backendUrl` is a host, we append the url to it.
**/
try {
return new URL(url, backendUrl).toString();
} catch (e) {
/*
* In case `backendUrl` is not qualified, we utilize `url` alone.
**/
return url;
}
};
const config = { const config = {
baseUrl: process.env.REACT_APP_BASE_URL, baseUrl: process.env.REACT_APP_BASE_URL,
graphqlUrl: process.env.REACT_APP_GRAPHQL_URL, graphqlUrl: computeUrl('/graphql', backendUrl),
restApiUrl: process.env.REACT_APP_REST_API_URL, restApiUrl: computeUrl('/api', backendUrl),
chatwootBaseUrl: 'https://app.chatwoot.com', chatwootBaseUrl: 'https://app.chatwoot.com',
supportEmailAddress: 'support@automatisch.io', supportEmailAddress: 'support@automatisch.io',
}; };
export default config; export default config;

View File

@@ -1,7 +1,9 @@
import { ApolloClient } from '@apollo/client'; import { ApolloClient } from '@apollo/client';
import cache from './cache'; import cache from './cache';
import createLink from './link'; import createLink from './link';
import appConfig from 'config/app'; import appConfig from 'config/app';
const client = new ApolloClient({ const client = new ApolloClient({
cache, cache,
link: createLink({ uri: appConfig.graphqlUrl }), link: createLink({ uri: appConfig.graphqlUrl }),
@@ -11,10 +13,19 @@ const client = new ApolloClient({
}, },
}, },
}); });
export function mutateAndGetClient(options) { export function mutateAndGetClient(options) {
const { onError, token } = options; const { onError, token } = options;
const link = createLink({ uri: appConfig.graphqlUrl, token, onError });
const link = createLink({
uri: appConfig.graphqlUrl,
token,
onError,
});
client.setLink(link); client.setLink(link);
return client; return client;
} }
export default client; export default client;