Skip to content

Commit

Permalink
fix(SLB-209): proxy graphql requests
Browse files Browse the repository at this point in the history
So it is possible to change Drupal's url via environment variables,
after build.
  • Loading branch information
pmelab committed Apr 12, 2024
1 parent 85f919a commit 2d4c5b4
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 34 deletions.
1 change: 1 addition & 0 deletions apps/preview/.lagoon.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DRUPAL_URL="https://${LAGOON_GIT_BRANCH}-${PROJECT_NAME}.cms.amazeelabs.dev"
2 changes: 2 additions & 0 deletions apps/preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@custom/ui": "workspace:*",
"express": "^4.19.2",
"express-ws": "^5.0.2",
"http-proxy-middleware": "^3.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rxjs": "^7.8.1"
Expand All @@ -25,6 +26,7 @@
"@swc/core": "^1.3.102",
"@types/express": "^4.17.21",
"@types/express-ws": "^3.0.4",
"@types/http-proxy-middleware": "^1.0.0",
"@types/node": "^20.11.17",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
Expand Down
9 changes: 9 additions & 0 deletions apps/preview/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from 'express';
import expressWs from 'express-ws';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { Subject } from 'rxjs';

const expressServer = express();
Expand All @@ -8,6 +9,14 @@ const { app } = expressWsInstance;

const updates$ = new Subject();

app.use(
'/graphql',
createProxyMiddleware({
target: process.env.DRUPAL_URL || 'http://localhost:8888',
changeOrigin: true,
}),
);

// TODO: Protect endpoints and preview with Drupal authentication.
app.post('/__preview', (req, res) => {
updates$.next({ body: req.body });
Expand Down
7 changes: 1 addition & 6 deletions apps/preview/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ function App() {
return sub.unsubscribe;
}, [refresh]);
return (
<OperationExecutor
executor={drupalExecutor(
`${import.meta.env.VITE_DRUPAL_URL || 'http://localhost:8888'}/graphql`,
false,
)}
>
<OperationExecutor executor={drupalExecutor('/graphql', false)}>
<Frame>
<Preview />
</Frame>
Expand Down
Loading

0 comments on commit 2d4c5b4

Please sign in to comment.