Created
May 18, 2023 18:34
-
-
Save enisdenjo/f1367294a2b3d740dfc501788ffc7e95 to your computer and use it in GitHub Desktop.
GraphiQL ❤️ graphql-http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
* 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> | |
<meta charset="utf-8" /> | |
<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.js" | |
type="application/javascript" | |
></script> | |
<script | |
src="https://unpkg.com/graphql-http/umd/graphql-http.js" | |
type="application/javascript" | |
></script> | |
<script> | |
const httpClient = graphqlHttp.createClient({ | |
url: "http://localhost:4000/graphql", | |
}); | |
function subscribe(payload) { | |
return new Promise((resolve, reject) => { | |
let result; | |
const dispose = httpClient.subscribe(payload, { | |
next: (data) => { | |
result = data; | |
}, | |
error: reject, | |
complete: () => { | |
resolve(result); | |
}, | |
}); | |
}); | |
} | |
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