Skip to content

Commit e520b6d

Browse files
committed
Avoid ruru dependency and restore GraphiQL in the exported middleware
1 parent 000542b commit e520b6d

File tree

4 files changed

+101
-22
lines changed

4 files changed

+101
-22
lines changed

bin/json-graphql-server.cjs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var path = require('path');
44
var express = require('express');
55
var cors = require('cors');
66
var JsonGraphqlServer = require('../dist/json-graphql-server-node.cjs').default;
7-
var ruru = require('ruru/server');
87
var dataFilePath = process.argv.length > 2 ? process.argv[2] : './data.json';
98
var data = require(path.join(process.cwd(), dataFilePath));
109
var PORT = process.env.NODE_PORT || 3000;
@@ -23,24 +22,7 @@ process.argv.forEach((arg, index) => {
2322
});
2423

2524
app.use(cors());
26-
const gqlServer = JsonGraphqlServer(data);
27-
const graphiql = (req, res) => {
28-
res.writeHead(200, undefined, {
29-
"Content-Type": "text/html; charset=utf-8",
30-
});
31-
return res.end(
32-
ruru.ruruHTML({
33-
endpoint: '/graphql',
34-
})
35-
);
36-
}
37-
app.use('/', (req, res) => {
38-
if (req.is('application/json')) {
39-
return gqlServer(req, res);
40-
}
41-
42-
return graphiql(req, res);
43-
});
25+
app.use('/', JsonGraphqlServer(data));
4426
app.listen(PORT, HOST);
4527
var msg = `GraphQL server running with your data at http://${HOST}:${PORT}/`;
4628
console.log(msg); // eslint-disable-line no-console

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"inflection": "^3.0.0",
6969
"lodash.merge": "^4.6.2",
7070
"reify": "^0.20.12",
71-
"ruru": "^2.0.0-beta.13",
7271
"xhr-mock": "^2.5.1"
7372
},
7473
"bin": {

src/graphiqlHandler.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
export const graphiqlHandler = (req, res) => {
2+
res.writeHead(200, undefined, {
3+
'Content-Type': 'text/html; charset=utf-8',
4+
});
5+
return res.end(
6+
getGraphiqlHtml({
7+
endpoint: '/',
8+
})
9+
);
10+
};
11+
12+
const getGraphiqlHtml = ({ endpoint }) => `
13+
<!--
14+
* Copyright (c) 2021 GraphQL Contributors
15+
* All rights reserved.
16+
* Copy of https://github.com/graphql/graphiql/blob/main/examples/graphiql-cdn/index.html
17+
* https://github.com/graphql/graphiql
18+
* https://github.com/graphql/graphiql/blob/main/LICENSE
19+
-->
20+
<!doctype html>
21+
<html lang="en">
22+
<head>
23+
<title>GraphiQL</title>
24+
<style>
25+
body {
26+
height: 100%;
27+
margin: 0;
28+
width: 100%;
29+
overflow: hidden;
30+
}
31+
32+
#graphiql {
33+
height: 100vh;
34+
}
35+
</style>
36+
<script
37+
crossorigin
38+
src="https://unpkg.com/react@18/umd/react.production.min.js"
39+
></script>
40+
<script
41+
crossorigin
42+
src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
43+
></script>
44+
<!--
45+
These two files can be found in the npm module, however you may wish to
46+
copy them directly into your environment, or perhaps include them in your
47+
favored resource bundler.
48+
-->
49+
<script
50+
src="https://unpkg.com/graphiql/graphiql.min.js"
51+
type="application/javascript"
52+
></script>
53+
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" />
54+
<!--
55+
These are imports for the GraphIQL Explorer plugin.
56+
-->
57+
<script
58+
src="https://unpkg.com/@graphiql/plugin-explorer/dist/index.umd.js"
59+
crossorigin
60+
></script>
61+
62+
<link
63+
rel="stylesheet"
64+
href="https://unpkg.com/@graphiql/plugin-explorer/dist/style.css"
65+
/>
66+
</head>
67+
68+
<body>
69+
<div id="graphiql">Loading...</div>
70+
<script>
71+
const root = ReactDOM.createRoot(document.getElementById('graphiql'));
72+
const fetcher = GraphiQL.createFetcher({
73+
url: '${endpoint}',
74+
headers: { 'X-Example-Header': 'foo' },
75+
});
76+
const explorerPlugin = GraphiQLPluginExplorer.explorerPlugin();
77+
root.render(
78+
React.createElement(GraphiQL, {
79+
fetcher,
80+
defaultEditorToolsVisibility: true,
81+
plugins: [explorerPlugin],
82+
}),
83+
);
84+
</script>
85+
</body>
86+
</html>`;

src/jsonGraphqlExpress.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createHandler } from 'graphql-http/lib/use/express';
22
import schemaBuilder from './schemaBuilder';
3+
import { graphiqlHandler } from './graphiqlHandler';
34

45
/**
56
* An express middleware for a GraphQL endpoint serving data from the supplied json.
@@ -45,7 +46,18 @@ import schemaBuilder from './schemaBuilder';
4546
*
4647
* app.listen(PORT);
4748
*/
48-
export default (data) =>
49-
createHandler({
49+
export default (data) => {
50+
const graphqlHandler = createHandler({
5051
schema: schemaBuilder(data),
5152
});
53+
54+
const expressMiddleware = (req, res) => {
55+
if (req.is('application/json')) {
56+
return graphqlHandler(req, res);
57+
}
58+
59+
return graphiqlHandler(req, res);
60+
};
61+
62+
return expressMiddleware;
63+
};

0 commit comments

Comments
 (0)