Updated graphiql handler to use authToken() where available

gql_cataclysm
noah metz 2023-10-10 22:25:12 -06:00
parent 0a936f50f8
commit c4de49099b
1 changed files with 26 additions and 12 deletions

@ -235,22 +235,36 @@ func GraphiQLHandler(auth_token string) func(http.ResponseWriter, *http.Request)
type="application/javascript" type="application/javascript"
></script> ></script>
<script> <script>
if (window.authToken === undefined) {
const root = ReactDOM.createRoot(document.getElementById('graphiql'));
root.render(
React.createElement(GraphiQL, {
fetcher: GraphiQL.createFetcher({
url: '/gql',
}),
defaultEditorToolsVisibility: true,
}),
);
} else {
authToken().then(function(res){
const root = ReactDOM.createRoot(document.getElementById('graphiql')); const root = ReactDOM.createRoot(document.getElementById('graphiql'));
root.render( root.render(
React.createElement(GraphiQL, { React.createElement(GraphiQL, {
fetcher: GraphiQL.createFetcher({ fetcher: GraphiQL.createFetcher({
url: '/gql', url: '/gql',
headers: { headers: {
"Authorization": "Basic %s", "Authorization": ` + "`Basic ${res}`" + `,
}, },
}), }),
defaultEditorToolsVisibility: true, defaultEditorToolsVisibility: true,
}), }),
); );
});
}
</script> </script>
</body> </body>
</html> </html>
`, auth_token) `)
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)