-
Notifications
You must be signed in to change notification settings - Fork 276
update swapi to use GraphiQL 5 with Monaco editor and React 19 #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ npm-debug.log | |
/cache/data.json | ||
|
||
# Local Netlify folder | ||
.netlify | ||
.netlify/ | ||
.idea/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,172 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="robots" content="noindex" /> | ||
<meta name="referrer" content="origin" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>SWAPI GraphQL API</title> | ||
<style> | ||
body { | ||
height: 100vh; | ||
margin: 0; | ||
overflow: hidden; | ||
} | ||
#splash { | ||
color: #333; | ||
display: flex; | ||
flex-direction: column; | ||
font-family: system, -apple-system, "San Francisco", | ||
".SFNSDisplay-Regular", "Segoe UI", Segoe, "Segoe WP", | ||
"Helvetica Neue", helvetica, "Lucida Grande", arial, sans-serif; | ||
height: 100vh; | ||
justify-content: center; | ||
text-align: center; | ||
} | ||
</style> | ||
<link rel="icon" href="favicon.ico" /> | ||
<link | ||
type="text/css" | ||
href="//unpkg.com/graphiql@4/graphiql.min.css" | ||
rel="stylesheet" | ||
/> | ||
<link | ||
type="text/css" | ||
href="//unpkg.com/@graphiql/plugin-explorer@4/dist/style.css" | ||
rel="stylesheet" | ||
/> | ||
</head> | ||
<body> | ||
<div id="splash">Loading…</div> | ||
<script src="//unpkg.com/[email protected]/umd/react.production.min.js"></script> | ||
<script src="//unpkg.com/[email protected]/umd/react-dom.production.min.js"></script> | ||
<script src="//unpkg.com/graphiql@4/graphiql.min.js"></script> | ||
<script src="//unpkg.com/@graphiql/plugin-explorer@4/dist/index.umd.js"></script> | ||
<script> | ||
// Parse the search string to get url parameters. | ||
var search = window.location.search; | ||
var parameters = {}; | ||
search | ||
.substr(1) | ||
.split("&") | ||
.forEach(function (entry) { | ||
var eq = entry.indexOf("="); | ||
if (eq >= 0) { | ||
parameters[decodeURIComponent(entry.slice(0, eq))] = | ||
decodeURIComponent(entry.slice(eq + 1)); | ||
} | ||
}); | ||
|
||
// if variables was provided, try to format it. | ||
if (parameters.variables) { | ||
try { | ||
parameters.variables = JSON.stringify( | ||
JSON.parse(parameters.variables), | ||
null, | ||
2 | ||
); | ||
} catch (e) { | ||
// Do nothing, we want to display the invalid JSON as a string, rather | ||
// than present an error. | ||
} | ||
} | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="robots" content="noindex" /> | ||
<meta name="referrer" content="origin" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>SWAPI GraphQL API</title> | ||
<style> | ||
body { | ||
margin: 0; | ||
} | ||
|
||
// When the query and variables string is edited, update the URL bar so | ||
// that it can be easily shared | ||
function onEditQuery(newQuery) { | ||
parameters.query = newQuery; | ||
updateURL(); | ||
} | ||
function onEditVariables(newVariables) { | ||
parameters.variables = newVariables; | ||
updateURL(); | ||
#graphiql { | ||
height: 100dvh; | ||
} | ||
|
||
.loading { | ||
height: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
font-size: 4rem; | ||
} | ||
</style> | ||
<link rel="icon" href="favicon.ico" /> | ||
<link rel="stylesheet" href="https://esm.sh/graphiql/dist/style.css" /> | ||
<link | ||
rel="stylesheet" | ||
href="https://esm.sh/@graphiql/plugin-explorer/dist/style.css" | ||
/> | ||
<!-- Note: the ?standalone flag bundles the module along with all of its `dependencies`, excluding `peerDependencies`, into a single JavaScript file. --> | ||
<!-- `@emotion/is-prop-valid` is a shim to remove the console error `module "@emotion /is-prop-valid" not found`. Upstream issue: https://github.com/motiondivision/motion/issues/3126 --> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"react": "https://esm.sh/[email protected]", | ||
"react/jsx-runtime": "https://esm.sh/[email protected]/jsx-runtime", | ||
|
||
"react-dom": "https://esm.sh/[email protected]", | ||
"react-dom/client": "https://esm.sh/[email protected]/client", | ||
"@emotion/is-prop-valid": "data:text/javascript,", | ||
|
||
"graphiql": "https://esm.sh/graphiql?standalone&external=react,react-dom,@graphiql/react,graphql", | ||
"@graphiql/plugin-explorer": "https://esm.sh/@graphiql/plugin-explorer?standalone&external=react,@graphiql/react,graphql", | ||
"@graphiql/react": "https://esm.sh/@graphiql/react?standalone&external=react,react-dom,graphql,@emotion/is-prop-valid", | ||
|
||
"@graphiql/toolkit": "https://esm.sh/@graphiql/toolkit?standalone&external=graphql", | ||
"graphql": "https://esm.sh/[email protected]" | ||
} | ||
function onEditOperationName(newOperationName) { | ||
parameters.operationName = newOperationName; | ||
updateURL(); | ||
} | ||
</script> | ||
<script type="module"> | ||
// Import React and ReactDOM | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
// Import GraphiQL and the Explorer plugin | ||
import { GraphiQL, HISTORY_PLUGIN } from 'graphiql'; | ||
import { createGraphiQLFetcher } from '@graphiql/toolkit'; | ||
import { explorerPlugin } from '@graphiql/plugin-explorer'; | ||
|
||
import createJSONWorker from 'https://esm.sh/monaco-editor/esm/vs/language/json/json.worker.js?worker'; | ||
import createGraphQLWorker from 'https://esm.sh/monaco-graphql/esm/graphql.worker.js?worker'; | ||
import createEditorWorker from 'https://esm.sh/monaco-editor/esm/vs/editor/editor.worker.js?worker'; | ||
|
||
globalThis.MonacoEnvironment = { | ||
getWorker(_workerId, label) { | ||
console.info('MonacoEnvironment.getWorker', { label }); | ||
switch (label) { | ||
case 'json': | ||
return createJSONWorker(); | ||
case 'graphql': | ||
return createGraphQLWorker(); | ||
} | ||
function updateURL() { | ||
var newSearch = | ||
"?" + | ||
Object.keys(parameters) | ||
.filter(function (key) { | ||
return Boolean(parameters[key]); | ||
}) | ||
.map(function (key) { | ||
return ( | ||
encodeURIComponent(key) + | ||
"=" + | ||
encodeURIComponent(parameters[key]) | ||
); | ||
}) | ||
.join("&"); | ||
history.replaceState(null, null, newSearch); | ||
return createEditorWorker(); | ||
}, | ||
}; | ||
|
||
// Parse the search string to get url parameters. | ||
var search = window.location.search; | ||
var parameters = {}; | ||
search | ||
.substr(1) | ||
.split('&') | ||
.forEach(function(entry) { | ||
var eq = entry.indexOf('='); | ||
if (eq >= 0) { | ||
parameters[decodeURIComponent(entry.slice(0, eq))] = | ||
decodeURIComponent(entry.slice(eq + 1)); | ||
} | ||
}); | ||
|
||
const fetcher = GraphiQL.createFetcher({ | ||
url: | ||
parameters.fetchURL || | ||
"/graphql", | ||
}); | ||
|
||
const explorerPlugin = GraphiQLPluginExplorer.explorerPlugin(); | ||
// Render <GraphiQL /> into the body. | ||
ReactDOM.render( | ||
React.createElement(GraphiQL, { | ||
fetcher: fetcher, | ||
query: parameters.query, | ||
variables: parameters.variables, | ||
operationName: parameters.operationName, | ||
onEditQuery: onEditQuery, | ||
onEditVariables: onEditVariables, | ||
onEditOperationName: onEditOperationName, | ||
plugins: [explorerPlugin] | ||
}), | ||
document.body | ||
// if variables was provided, try to format it. | ||
if (parameters.variables) { | ||
try { | ||
parameters.variables = JSON.stringify( | ||
JSON.parse(parameters.variables), | ||
null, | ||
2, | ||
); | ||
</script> | ||
</body> | ||
} catch (e) { | ||
// Do nothing, we want to display the invalid JSON as a string, rather | ||
// than present an error. | ||
} | ||
} | ||
|
||
// When the query and variables string is edited, update the URL bar so | ||
// that it can be easily shared | ||
function onEditQuery(newQuery) { | ||
parameters.query = newQuery; | ||
updateURL(); | ||
} | ||
|
||
function onEditVariables(newVariables) { | ||
parameters.variables = newVariables; | ||
updateURL(); | ||
} | ||
|
||
function onEditOperationName(newOperationName) { | ||
parameters.operationName = newOperationName; | ||
updateURL(); | ||
} | ||
|
||
function updateURL() { | ||
var newSearch = | ||
'?' + | ||
Object.keys(parameters) | ||
.filter(function(key) { | ||
return Boolean(parameters[key]); | ||
}) | ||
.map(function(key) { | ||
return ( | ||
encodeURIComponent(key) + | ||
'=' + | ||
encodeURIComponent(parameters[key]) | ||
); | ||
}) | ||
.join('&'); | ||
history.replaceState(null, null, newSearch); | ||
} | ||
|
||
const fetcher = createGraphiQLFetcher({ | ||
url: | ||
parameters.fetchURL || | ||
'/graphql', | ||
}); | ||
const plugins = [HISTORY_PLUGIN, explorerPlugin()]; | ||
|
||
function App() { | ||
return React.createElement(GraphiQL, { | ||
fetcher: fetcher, | ||
initialQuery: parameters.query, | ||
initialVariables: parameters.variables, | ||
operationName: parameters.operationName, | ||
onEditQuery: onEditQuery, | ||
onEditVariables: onEditVariables, | ||
onEditOperationName: onEditOperationName, | ||
plugins, | ||
}); | ||
} | ||
|
||
const container = document.getElementById('graphiql'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this works despite the call being before the element exists because it's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah it's because |
||
const root = ReactDOM.createRoot(container); | ||
root.render(React.createElement(App)); | ||
</script> | ||
</head> | ||
<body> | ||
<div id="graphiql"> | ||
<div class="loading">Loading…</div> | ||
</div> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
<script>
tag is neither in<body>
nor<head>
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great catch, didn't move lines enough from
<body>