Skip to content

Instantly share code, notes, and snippets.

@AugustoCalaca
Forked from enisdenjo/graphiql-over-ws.html
Created January 14, 2022 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AugustoCalaca/7e37b0abf722e766bb88aa5c06df08d5 to your computer and use it in GitHub Desktop.
Save AugustoCalaca/7e37b0abf722e766bb88aa5c06df08d5 to your computer and use it in GitHub Desktop.
GraphiQL ❤️ graphql-ws
<!--
* Copyright (c) 2021 GraphQL Contributors
* All rights reserved.
*
* This code is licensed under the MIT license.
* Use it however you wish.
-->
<!DOCTYPE html>
<html>
<head>
<style>
body {
height: 100%;
margin: 0;
width: 100%;
overflow: hidden;
}
#graphiql {
height: 100vh;
}
</style>
<!--
This GraphiQL example depends on Promise and AsyncIterator, 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@16/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@16/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/graphql-ws/umd/graphql-ws.min.js"
type="application/javascript"
></script>
<script>
const wsClient = graphqlWs.createClient({
url: "ws://localhost:4000/graphql",
lazy: false, // connect as soon as the page opens
});
function subscribe(payload) {
let deferred = null;
const pending = [];
let throwMe = null,
done = false;
const dispose = wsClient.subscribe(payload, {
next: (data) => {
pending.push(data);
deferred?.resolve(false);
},
error: (err) => {
if (err instanceof Error) {
throwMe = err;
} else if (err instanceof CloseEvent) {
throwMe = new Error(
`Socket closed with event ${err.code} ${
err.reason || ""
}`.trim()
);
} else {
// GraphQLError[]
throwMe = new Error(err.map(({ message }) => message).join(", "));
}
deferred?.reject(throwMe);
},
complete: () => {
done = true;
deferred?.resolve(true);
},
});
return {
[Symbol.asyncIterator]() {
return this;
},
async next() {
if (done) return { done: true, value: undefined };
if (throwMe) throw throwMe;
if (pending.length) return { value: pending.shift() };
return (await new Promise(
(resolve, reject) => (deferred = { resolve, reject })
))
? { done: true, value: undefined }
: { value: pending.shift() };
},
async return() {
dispose();
return { done: true, value: undefined };
},
};
}
ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: subscribe,
defaultVariableEditorOpen: true,
}),
document.getElementById("graphiql")
);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment