Skip to content

Instantly share code, notes, and snippets.

@nksaraf
Last active December 10, 2020 01:02
Show Gist options
  • Save nksaraf/02f2e67cdd500bd4c92a6a30674f55ba to your computer and use it in GitHub Desktop.
Save nksaraf/02f2e67cdd500bd4c92a6a30674f55ba to your computer and use it in GitHub Desktop.
GraphiQL with Explorer for servers
<!DOCTYPE html>
<html>
<head>
<style>
body {
height: 100vh;
margin: 0;
width: 100vw;
overflow: hidden;
}
</style>
<!--
This GraphiQL example depends on Promise and fetch, which are available in
modern browsers, but can be "polyfilled" for older browsers.
GraphiQL itself depends on React DOM.
If you do not want to rely on a CDN, you can host these files locally or
include them directly in your favored resource bunder.
-->
<script
crossorigin
src="https://unpkg.com/react@17/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
></script>
<!--
These two files can be found in the npm module, however you may wish to
copy them directly into your environment, or perhaps include them in your
favored resource bundler.
-->
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" />
</head>
<body>
<div id="graphiql">Loading...</div>
<script
src="https://unpkg.com/graphiql/graphiql.min.js"
type="application/javascript"
></script>
<script
src="https://unpkg.com/graphiql-explorer/graphiqlExplorer.min.js"
type="application/javascript"
></script>
<script
src="https://unpkg.com/htm/dist/htm.umd.js"
type="application/javascript"
></script>
<script type="module">
import ow from 'https://unpkg.com/oceanwind';
const { React, ReactDOM, htm, GraphiQL, GraphiQLExplorer } = window;
const html = htm.bind(React.createElement);
const getFetcher = (url) => function graphQLFetcher(graphQLParams) {
return fetch(
url,
{
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(graphQLParams),
},
).then(function (response) {
return response.json().catch(function () {
return response.text();
});
});
}
function GraphiQLPlayground({ url }) {
const [query, setQuery] = React.useState(``);
const graphiqlRef = React.useRef();
const [graphiql, setGraphiqlState] = React.useState({});
const fetcher = React.useMemo(() => getFetcher(url), [url])
React.useEffect(() => {
const originalSetState = graphiqlRef.current.safeSetState;
setGraphiqlState(graphiqlRef.current.state)
graphiqlRef.current.safeSetState = (fn) => {
setGraphiqlState(prev => ({ ...prev, ...fn }));
originalSetState(fn)
};
}, [graphiqlRef.current])
return html`
<div className=${ow`flex flex-row flex-1`} key=${url}>
<${GraphiQLExplorer.Explorer}
query=${query}
onEdit=${setQuery}
explorerIsOpen=${true}
schema=${graphiql.schema}
width=${240}
/>
<${GraphiQL}
ref=${(ref) => graphiqlRef.current = ref}
fetcher=${fetcher}
query=${query}
onEditQuery=${setQuery}
defaultVariableEditorOpen=${true}
/>
</div>
`
}
const GRAPHQL_API_URL = '/api/graphql';
function Playground() {
const [url, setUrl] = React.useState(GRAPHQL_API_URL);
return html`
<div className=${ow`flex flex-col h-screen w-screen`}>
<div><input type="text" defaultValue=${url} onBlur=${(e) => {
setUrl(e.currentTarget.value)
}} className=${ow`w-full`} /></div>
<${GraphiQLPlayground} url=${url} key=${url} />
</div>
`
}
ReactDOM.render(
html`<${Playground} />`,
document.getElementById('graphiql'),
);
</script>
<script>
</script>
</body>
</html>
export default ({
endpoint,
defaultQuery = "",
defaultVariableEditorOpen = false,
headers = {},
headerEditorEnabled = false,
subscriptionsEndpoint = null,
}) => `<!DOCTYPE html>
<html>
<head>
<style>
body {
height: 100vh;
margin: 0;
width: 100vw;
overflow: hidden;
}
</style>
<script
crossorigin
src="https://unpkg.com/react@17/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
></script>
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" />
</head>
<body>
<div id="graphiql">Loading...</div>
<script
src="https://unpkg.com/graphiql/graphiql.min.js"
type="application/javascript"
></script>
<script
src="https://unpkg.com/graphiql-explorer/graphiqlExplorer.min.js"
type="application/javascript"
></script>
<script
src="https://unpkg.com/htm/dist/htm.umd.js"
type="application/javascript"
></script>
<script type="module">
import ow from 'https://unpkg.com/oceanwind';
const { React, ReactDOM, htm, GraphiQL, GraphiQLExplorer } = window;
const html = htm.bind(React.createElement);
const getFetcher = (endpoint) => function graphQLFetcher(graphQLParams) {
return fetch(
endpoint,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...JSON.parse("${JSON.stringify(headers)}")
},
body: JSON.stringify(graphQLParams),
},
).then(function (response) {
return response.json().catch(function () {
return response.text();
});
});
}
function GraphiQLPlayground({ url }) {
const [query, setQuery] = React.useState(\`${defaultQuery}\`);
const graphiqlRef = React.useRef();
const [graphiql, setGraphiqlState] = React.useState({});
const fetcher = React.useMemo(() => getFetcher(url), [url])
React.useEffect(() => {
const originalSetState = graphiqlRef.current.safeSetState;
setGraphiqlState(graphiqlRef.current.state)
graphiqlRef.current.safeSetState = (fn) => {
setGraphiqlState(prev => ({ ...prev, ...fn }));
originalSetState(fn)
};
}, [graphiqlRef.current])
return html\`
<div className=\${ow\`flex flex-row flex-1\`} key=\${url}>
<\${GraphiQLExplorer.Explorer}
query=\${query}
onEdit=\${setQuery}
explorerIsOpen=\${true}
schema=\${graphiql.schema}
width=\${240}
/>
<\${GraphiQL}
ref=\${(ref) => graphiqlRef.current = ref}
fetcher=\${fetcher}
query=\${query}
onEditQuery=\${setQuery}
defaultVariableEditorOpen=\${${defaultVariableEditorOpen}}
/>
</div>
\`
}
const GRAPHQL_API_URL = \`${endpoint}\`;
function Playground() {
const [url, setUrl] = React.useState(GRAPHQL_API_URL);
return html\`
<div className=\${ow\`flex flex-col h-screen w-screen\`}>
<div><input type="text" defaultValue=\${url} onBlur=\${(e) => {
setUrl(e.currentTarget.value)
}} className=\${ow\`w-full\`} /></div>
<\${GraphiQLPlayground} url=\${url} key=\${url} />
</div>
\`
}
ReactDOM.render(
html\`<\${Playground} />\`,
document.getElementById('graphiql'),
);
</script>
</body>
</html>`;
module.exports = ({
endpoint,
defaultQuery = "",
defaultVariableEditorOpen = false,
headers = {},
headerEditorEnabled = false,
subscriptionsEndpoint = null,
}) => `<!DOCTYPE html>
<html>
<head>
<style>
body {
height: 100vh;
margin: 0;
width: 100vw;
overflow: hidden;
}
</style>
<script
crossorigin
src="https://unpkg.com/react@17/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
></script>
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" />
</head>
<body>
<div id="graphiql">Loading...</div>
<script
src="https://unpkg.com/graphiql/graphiql.min.js"
type="application/javascript"
></script>
<script
src="https://unpkg.com/graphiql-explorer/graphiqlExplorer.min.js"
type="application/javascript"
></script>
<script
src="https://unpkg.com/htm/dist/htm.umd.js"
type="application/javascript"
></script>
<script type="module">
import ow from 'https://unpkg.com/oceanwind';
const { React, ReactDOM, htm, GraphiQL, GraphiQLExplorer } = window;
const html = htm.bind(React.createElement);
const getFetcher = (endpoint) => function graphQLFetcher(graphQLParams) {
return fetch(
endpoint,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...JSON.parse("${JSON.stringify(headers)}")
},
body: JSON.stringify(graphQLParams),
},
).then(function (response) {
return response.json().catch(function () {
return response.text();
});
});
}
function GraphiQLPlayground({ url }) {
const [query, setQuery] = React.useState(\`${defaultQuery}\`);
const graphiqlRef = React.useRef();
const [graphiql, setGraphiqlState] = React.useState({});
const fetcher = React.useMemo(() => getFetcher(url), [url])
React.useEffect(() => {
const originalSetState = graphiqlRef.current.safeSetState;
setGraphiqlState(graphiqlRef.current.state)
graphiqlRef.current.safeSetState = (fn) => {
setGraphiqlState(prev => ({ ...prev, ...fn }));
originalSetState(fn)
};
}, [graphiqlRef.current])
return html\`
<div className=\${ow\`flex flex-row flex-1\`} key=\${url}>
<\${GraphiQLExplorer.Explorer}
query=\${query}
onEdit=\${setQuery}
explorerIsOpen=\${true}
schema=\${graphiql.schema}
width=\${240}
/>
<\${GraphiQL}
ref=\${(ref) => graphiqlRef.current = ref}
fetcher=\${fetcher}
query=\${query}
onEditQuery=\${setQuery}
defaultVariableEditorOpen=\${${defaultVariableEditorOpen}}
/>
</div>
\`
}
const GRAPHQL_API_URL = \`${endpoint}\`;
function Playground() {
const [url, setUrl] = React.useState(GRAPHQL_API_URL);
return html\`
<div className=\${ow\`flex flex-col h-screen w-screen\`}>
<div><input type="text" defaultValue=\${url} onBlur=\${(e) => {
setUrl(e.currentTarget.value)
}} className=\${ow\`w-full\`} /></div>
<\${GraphiQLPlayground} url=\${url} key=\${url} />
</div>
\`
}
ReactDOM.render(
html\`<\${Playground} />\`,
document.getElementById('graphiql'),
);
</script>
</body>
</html>`;
{
"name": "graphiql-playground",
"version": "0.0.1",
"main": "index.js",
"module": "index.esm.js"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment