Skip to content

Commit

Permalink
Add query explorer plugin to GraphiQL (#1470)
Browse files Browse the repository at this point in the history
* Add html syntax highlights

* Add graphiql-plugin-explorer

* Improve placeholder

* Changesets

* Refactor to use CSS classes
  • Loading branch information
frandiox authored Oct 31, 2023
1 parent bbd8cc6 commit 8198c1b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-colts-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Add query explorer plugin to GraphiQL.
179 changes: 110 additions & 69 deletions packages/hydrogen/src/routing/graphiql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,76 +20,117 @@ export const graphiqlLoader: GraphiQLLoader = async function graphiqlLoader({
// GraphiQL icon from their GitHub repo
const favicon = `https://avatars.githubusercontent.com/u/12972006?s=48&v=4`;

// Add code highlighting to the HTML template
const html = String.raw;

return new Response(
`
<!DOCTYPE html>
<html lang="en">
<head>
<title>GraphiQL</title>
<link rel="icon" type="image/x-icon" href="${favicon}">
<style>
body {
height: 100%;
margin: 0;
width: 100%;
overflow: hidden;
}
#graphiql {
height: 100vh;
}
</style>
<script
crossorigin
src="https://unpkg.com/react@18/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"
></script>
<link rel="stylesheet" href="https://unpkg.com/graphiql@3/graphiql.min.css" />
</head>
<body>
<div id="graphiql">Loading...</div>
<script
src="https://unpkg.com/graphiql@3/graphiql.min.js"
type="application/javascript"
></script>
<script>
const windowUrl = new URL(document.URL);
let query = '{\\n shop {\\n name\\n }\\n}';
if (windowUrl.searchParams.has('query')) {
query = decodeURIComponent(windowUrl.searchParams.get('query') ?? '');
// Prettify query
if (query) query = GraphiQL.GraphQL.print(GraphiQL.GraphQL.parse(query));
}
let variables;
if (windowUrl.searchParams.has('variables')) {
variables = decodeURIComponent(windowUrl.searchParams.get('variables') ?? '');
// Prettify variables
if (variables) variables = JSON.stringify(JSON.parse(variables), null, 2);
}
const root = ReactDOM.createRoot(document.getElementById('graphiql'));
root.render(
React.createElement(GraphiQL, {
fetcher: GraphiQL.createFetcher({
url: '${url}',
headers: {'X-Shopify-Storefront-Access-Token': '${accessToken}'}
}),
defaultEditorToolsVisibility: true,
query,
variables
}),
);
</script>
</body>
</html>
`,
html`
<!DOCTYPE html>
<html lang="en">
<head>
<title>GraphiQL</title>
<link rel="icon" type="image/x-icon" href="${favicon}" />
<style>
body {
height: 100%;
margin: 0;
width: 100%;
overflow: hidden;
background-color: hsl(219, 29%, 18%);
}
#graphiql {
height: 100vh;
}
#graphiql > .placeholder {
color: slategray;
width: fit-content;
margin: 40px auto;
font-family: Arial;
}
</style>
<script
crossorigin
src="https://unpkg.com/react@18/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"
></script>
<link
rel="stylesheet"
href="https://unpkg.com/graphiql@3/graphiql.min.css"
/>
<link
rel="stylesheet"
href="https://unpkg.com/@graphiql/plugin-explorer/dist/style.css"
/>
</head>
<body>
<div id="graphiql">
<div class="placeholder">Loading GraphiQL...</div>
</div>
<script
src="https://unpkg.com/graphiql@3/graphiql.min.js"
type="application/javascript"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/@graphiql/plugin-explorer/dist/index.umd.js"
type="application/javascript"
crossorigin="anonymous"
></script>
<script>
const windowUrl = new URL(document.URL);
let query = '{ shop { name } }';
if (windowUrl.searchParams.has('query')) {
query = decodeURIComponent(
windowUrl.searchParams.get('query') ?? query,
);
}
// Prettify query
query = GraphiQL.GraphQL.print(GraphiQL.GraphQL.parse(query));
let variables;
if (windowUrl.searchParams.has('variables')) {
variables = decodeURIComponent(
windowUrl.searchParams.get('variables') ?? '',
);
}
// Prettify variables
if (variables) {
variables = JSON.stringify(JSON.parse(variables), null, 2);
}
const root = ReactDOM.createRoot(
document.getElementById('graphiql'),
);
root.render(
React.createElement(GraphiQL, {
fetcher: GraphiQL.createFetcher({
url: '${url}',
headers: {
'X-Shopify-Storefront-Access-Token': '${accessToken}',
},
}),
defaultEditorToolsVisibility: true,
query,
variables,
plugins: [GraphiQLPluginExplorer.explorerPlugin()],
}),
);
</script>
</body>
</html>
`,
{status: 200, headers: {'content-type': 'text/html'}},
);
};

0 comments on commit 8198c1b

Please sign in to comment.