Skip to content

Instantly share code, notes, and snippets.

@duanebester
Created May 11, 2021 12:04
Show Gist options
  • Save duanebester/1c38cd4497ed17bc82c43f1134bce812 to your computer and use it in GitHub Desktop.
Save duanebester/1c38cd4497ed17bc82c43f1134bce812 to your computer and use it in GitHub Desktop.
Voyager GraphQL With API Key Prompt
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/react@@16/umd/react.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom@@16/umd/react-dom.production.min.js"></script>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.css"
/>
<script src="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.min.js"></script>
<style>
body {
padding: 0;
margin: 0;
width: 100%;
height: 100vh;
overflow: hidden;
}
</style>
</head>
<body>
<div id="voyager">Loading...</div>
<script>
var token;
var key = 'ff-voyager-info';
var currentMillis = new Date().getTime();
var potentialVoyagerInfo = window.localStorage.getItem(key);
var _8hours = 8 * 60 * 60 * 1000;
if (potentialVoyagerInfo) {
voyagerInfo = JSON.parse(potentialVoyagerInfo);
if((currentMillis - voyagerInfo.created) > _8hours) {
window.localStorage.removeItem(key);
} else {
token = voyagerInfo.token;
}
}
if (!token) {
token = prompt("Please Enter API Token");
var voyagerInfo = {
token: token,
created: currentMillis
};
window.localStorage.setItem(key, JSON.stringify(voyagerInfo));
}
function introspectionProvider(query) {
return fetch(window.location.origin + '/graphql', {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer " + token,
'Csrf-Token': 'nocheck-allow'
},
body: JSON.stringify({ query: query }),
}).then((response) => response.json());
}
GraphQLVoyager.init(document.getElementById('voyager'), {
introspection: introspectionProvider,
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment