Skip to content

Commit

Permalink
Merge pull request #3969 from Shopify/allow-cors-graphiql
Browse files Browse the repository at this point in the history
Allow CORS for checking if GraphiQL is running
  • Loading branch information
amcaplan authored Jun 4, 2024
2 parents 8f225ad + 5f10367 commit 053d71b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/new-hats-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/app': patch
---

Allow CORS to check if local GraphiQL is running
12 changes: 11 additions & 1 deletion packages/app/src/cli/services/dev/graphiql/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class TokenRefreshError extends AbortError {
}
}

function corsMiddleware(_req: express.Request, res: express.Response, next: (err?: Error) => unknown) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS')
res.setHeader(
'Access-Control-Allow-Headers',
'Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, ngrok-skip-browser-warning',
)
next()
}

interface SetupGraphiQLServerOptions {
stdout: Writable
port: number
Expand Down Expand Up @@ -85,7 +95,7 @@ export function setupGraphiQLServer({
}
}

app.get('/graphiql/ping', (_req, res) => {
app.get('/graphiql/ping', corsMiddleware, (_req, res) => {
res.send('pong')
})

Expand Down

0 comments on commit 053d71b

Please sign in to comment.