-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(playground): enable GraphQL playground using GraphiQL (#67)
- Loading branch information
Showing
13 changed files
with
254 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
"wrangler": "^3.0.0" | ||
}, | ||
"dependencies": { | ||
"edgeql": "^0.1.9", | ||
"edgeql": "0.2.2", | ||
"graphql": "^16.8.1" | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# EdgeQL GraphiQL middleware | ||
|
||
```typescript | ||
|
||
import type { Context } from 'edgeql' | ||
import { cors } from 'edgeql/cors' | ||
import { graphiql } from 'edgeql/graphiql' | ||
import { NodeEdgeQL } from 'edgeql/node' | ||
|
||
const app = new NodeEdgeQL() | ||
|
||
app.handle(` | ||
type Query { | ||
hello: String! | ||
} | ||
`, (ctx: Context) => { | ||
return `hello world` | ||
} | ||
) | ||
|
||
app.use(graphiql({path: '/playground'})) | ||
|
||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { EdgeQL } from '../../' | ||
import { graphiql } from '.' | ||
|
||
describe('GraphiQL', () => { | ||
it('should load GraphiQL when access the path', async () => { | ||
const app = new EdgeQL() | ||
app.use(graphiql({ path: '/playground' })) | ||
const endpoint = 'http://localhost' | ||
const req = new Request(endpoint + '/playground') | ||
const res = await app.fetch(req) | ||
expect(res.statusText).toEqual('') | ||
expect(res.status).toBe(200) | ||
expect(await res.text()).toEqual(` | ||
<html lang="en"> | ||
<head> | ||
<title>GraphiQL</title> | ||
<style> | ||
body { | ||
height: 100%; | ||
margin: 0; | ||
width: 100%; | ||
overflow: hidden; | ||
} | ||
#graphiql { | ||
height: 100vh; | ||
} | ||
</style> | ||
<script | ||
crossorigin | ||
src="https://unpkg.com/react@18/umd/react.development.js" | ||
></script> | ||
<script | ||
crossorigin | ||
src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" | ||
></script> | ||
<script | ||
src="https://unpkg.com/graphiql/graphiql.min.js" | ||
type="application/javascript" | ||
></script> | ||
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" /> | ||
<script | ||
src="https://unpkg.com/@graphiql/plugin-explorer/dist/index.umd.js" | ||
crossorigin | ||
></script> | ||
<link | ||
rel="stylesheet" | ||
href="https://unpkg.com/@graphiql/plugin-explorer/dist/style.css" | ||
/> | ||
</head> | ||
<body> | ||
<div id="graphiql">Loading...</div> | ||
<script> | ||
const root = ReactDOM.createRoot(document.getElementById('graphiql')); | ||
const fetcher = GraphiQL.createFetcher({ | ||
url: '${endpoint}', | ||
headers: { 'X-Example-Header': 'foo' }, | ||
}); | ||
const explorerPlugin = GraphiQLPluginExplorer.explorerPlugin(); | ||
root.render( | ||
React.createElement(GraphiQL, { | ||
fetcher, | ||
defaultEditorToolsVisibility: true, | ||
plugins: [explorerPlugin], | ||
}), | ||
); | ||
</script> | ||
</body> | ||
</html> | ||
`) | ||
}) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { Context, Middleware, Next } from '../../' | ||
|
||
export const graphiql = (options?: { path?: string }): Middleware => { | ||
const p = options?.path ?? '/playground' | ||
|
||
return async (ctx: Context, next: Next) => { | ||
const url = new URL(ctx.http.request.url) | ||
if (url.pathname !== p) { | ||
await next() | ||
} else { | ||
const endpoint = `${url.protocol}//${url.hostname}${url.port ? `:${url.port}` : ''}` | ||
ctx.http.status = 200 | ||
ctx.http.headers.set('content-type', 'text/html') | ||
ctx.http.body = ` | ||
<html lang="en"> | ||
<head> | ||
<title>GraphiQL</title> | ||
<style> | ||
body { | ||
height: 100%; | ||
margin: 0; | ||
width: 100%; | ||
overflow: hidden; | ||
} | ||
#graphiql { | ||
height: 100vh; | ||
} | ||
</style> | ||
<script | ||
crossorigin | ||
src="https://unpkg.com/react@18/umd/react.development.js" | ||
></script> | ||
<script | ||
crossorigin | ||
src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" | ||
></script> | ||
<script | ||
src="https://unpkg.com/graphiql/graphiql.min.js" | ||
type="application/javascript" | ||
></script> | ||
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" /> | ||
<script | ||
src="https://unpkg.com/@graphiql/plugin-explorer/dist/index.umd.js" | ||
crossorigin | ||
></script> | ||
<link | ||
rel="stylesheet" | ||
href="https://unpkg.com/@graphiql/plugin-explorer/dist/style.css" | ||
/> | ||
</head> | ||
<body> | ||
<div id="graphiql">Loading...</div> | ||
<script> | ||
const root = ReactDOM.createRoot(document.getElementById('graphiql')); | ||
const fetcher = GraphiQL.createFetcher({ | ||
url: '${endpoint}', | ||
headers: { 'X-Example-Header': 'foo' }, | ||
}); | ||
const explorerPlugin = GraphiQLPluginExplorer.explorerPlugin(); | ||
root.render( | ||
React.createElement(GraphiQL, { | ||
fetcher, | ||
defaultEditorToolsVisibility: true, | ||
plugins: [explorerPlugin], | ||
}), | ||
); | ||
</script> | ||
</body> | ||
</html> | ||
` | ||
} | ||
} | ||
} |