Skip to content

Commit

Permalink
feat: support setting explorer query on url
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed Aug 26, 2024
1 parent b80ece7 commit 4fce05a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
8 changes: 7 additions & 1 deletion api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ export const createApp = (): { app: Express; router: express.Router } => {
const basePath =
process.env.BASE_PATH === '/' ? '' : process.env.BASE_PATH || '';
const endpoint = `${basePath}${req.params.driveId !== undefined ? `/d/${req.params.driveId}` : '/drives'}`;
res.send(renderGraphqlPlayground(endpoint));

const { query } = req.query;
if (query && typeof query !== 'string') {
throw new Error('Invalid query');
}

res.send(renderGraphqlPlayground(endpoint, query));
});

// Hooks
Expand Down
40 changes: 27 additions & 13 deletions api/src/graphql/playground.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export function renderGraphqlPlayground(
url: string,
query?: string,
headers: Record<string, string> = {}
): string {
return `<!doctype html>
Expand Down Expand Up @@ -44,19 +45,32 @@ export function renderGraphqlPlayground(
<body>
<div id="graphiql">Loading...</div>
<script>
const root = ReactDOM.createRoot(document.getElementById('graphiql'));
const fetcher = GraphiQL.createFetcher({
url: '${url}',
headers: ${JSON.stringify(headers)},
});
const explorerPlugin = GraphiQLPluginExplorer.explorerPlugin();
root.render(
React.createElement(GraphiQL, {
fetcher,
defaultEditorToolsVisibility: true,
plugins: [explorerPlugin],
}),
);
var fetcher = GraphiQL.createFetcher({
url: '${url}',
headers: ${JSON.stringify(headers)}
});
var defaultQuery = ${query ? `\`${query}\`` : undefined};
if (defaultQuery) {
var sessionQuery = localStorage.getItem("graphiql:query");
if (sessionQuery) {
localStorage.setItem("graphiql:query", defaultQuery);
}
}
var explorerPlugin = GraphiQLPluginExplorer.explorerPlugin();
function GraphiQLWithExplorer() {
return React.createElement(GraphiQL, {
fetcher: fetcher,
defaultEditorToolsVisibility: true,
plugins: [explorerPlugin],
defaultQuery
});
}
const root = ReactDOM.createRoot(document.getElementById('graphiql'));
root.render(React.createElement(GraphiQLWithExplorer));
</script>
</body>
</html>`;
Expand Down

0 comments on commit 4fce05a

Please sign in to comment.