Skip to content

Commit 0780a1f

Browse files
authored
Merge pull request #178 from marmelab/modernize
Modernize
2 parents 3dacec7 + f9e76ad commit 0780a1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1752
-4728
lines changed

.eslintrc

-34
This file was deleted.

.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ jobs:
1919
- uses: bahmutov/npm-install@v1
2020
with:
2121
install-command: yarn --immutable
22+
- name: Code quality
23+
run: make lint format
2224
- name: Unit Tests
2325
run: make test
2426
env:

Makefile

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ watch: ## continuously compile ES6 files to JS
1313
@yarn vite build --watch
1414

1515
test: ## Launch unit tests
16-
@NODE_ENV=test NODE_OPTIONS="$$NODE_OPTIONS --experimental-vm-modules" ./node_modules/.bin/jest
16+
@yarn run test
1717

1818
watch-test: ## Launch unit tests and watch for changes
19-
@NODE_ENV=test NODE_OPTIONS="$$NODE_OPTIONS --experimental-vm-modules" ./node_modules/.bin/jest --watch
19+
@yarn run watch-test
20+
21+
check: ## Lint and format the source code
22+
@yarn run check
23+
24+
lint: ## Lint the source code
25+
@yarn run lint
2026

2127
format: ## Format the source code
22-
@./node_modules/.bin/eslint --fix ./src
28+
@yarn run format
2329

2430
run: ## Launch server with example data
25-
@node ./bin/json-graphql-server.cjs example/data.js
31+
@yarn run server
2632

2733
build: ## Build production release
28-
@yarn vite build
29-
@yarn vite build -c ./vite.config.node.js
30-
@yarn vite build -c ./vite.config.umd.js
34+
@yarn run build

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Note that the server is [GraphiQL](https://github.com/graphql/graphiql) enabled,
102102
## Install
103103

104104
```sh
105-
npm install json-graphql-server
105+
npm install -D json-graphql-server
106106
```
107107

108108
## Generated Types and Queries
@@ -460,7 +460,7 @@ const data = {
460460
// ... your data
461461
};
462462

463-
app.use('/graphql', jsonGraphqlExpress.default(data));
463+
app.use('/graphql', jsonGraphqlExpress(data));
464464
app.listen(PORT);
465465
```
466466

bin/json-graphql-server.cjs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/usr/bin/env node
2-
require('reify');
32
var path = require('path');
43
var express = require('express');
54
var cors = require('cors');
6-
var JsonGraphqlServer = require('../dist/json-graphql-server-node.cjs').default;
5+
var { jsonGraphqlExpress } = require('../dist/json-graphql-server-node.cjs');
76
var dataFilePath = process.argv.length > 2 ? process.argv[2] : './data.json';
87
var data = require(path.resolve(process.cwd(), dataFilePath));
98
var PORT = process.env.NODE_PORT || 3000;
@@ -22,7 +21,7 @@ process.argv.forEach((arg, index) => {
2221
});
2322

2423
app.use(cors());
25-
app.use('/', JsonGraphqlServer(data));
24+
app.use('/', jsonGraphqlExpress(data));
2625
app.listen(PORT, HOST);
2726
var msg = `GraphQL server running with your data at http://${HOST}:${PORT}/`;
2827
console.log(msg); // eslint-disable-line no-console

biome.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.7.0/schema.json",
3+
"organizeImports": {
4+
"enabled": true
5+
},
6+
"formatter": {
7+
"enabled": true,
8+
"indentStyle": "space",
9+
"indentWidth": 4,
10+
"ignore": ["public/*.js"]
11+
},
12+
"javascript": {
13+
"formatter": {
14+
"quoteStyle": "single"
15+
}
16+
},
17+
"linter": {
18+
"enabled": true,
19+
"rules": {
20+
"recommended": true,
21+
"suspicious": {
22+
"noExplicitAny": "off"
23+
}
24+
},
25+
"ignore": ["public/*.js"]
26+
}
27+
}

example/index.html example/browser/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</head>
66
<body>
77
<button id="button">Load posts</button>
8-
<script src="../dist/json-graphql-server.umd.cjs"></script>
8+
<script src="../../dist/json-graphql-server.umd.cjs"></script>
99
<script type="text/javascript">
1010
window.addEventListener('load', function() {
1111
const data = {

example/data.js example/data.cjs

File renamed without changes.

example/node/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Node example
2+
3+
Run it with `node index.mjs`

example/node/index.mjs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import express from 'express';
2+
import jsonGraphqlExpress from '../../dist/json-graphql-server-node.js';
3+
import data from '../data.cjs';
4+
5+
const PORT = 3000;
6+
const app = express();
7+
8+
app.use('/graphql', jsonGraphqlExpress(data));
9+
app.listen(PORT);

package.json

+28-30
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,45 @@
2727
],
2828
"license": "MIT",
2929
"scripts": {
30-
"format": "make format",
30+
"check": "biome check",
31+
"format": "biome format --write src",
32+
"lint": "biome lint --write src",
3133
"precommit": "lint-staged",
32-
"test": "jest",
33-
"watch-test": "make watch-test",
34-
"server": "make run",
35-
"prepublish": "make build"
34+
"test": "cross-env NODE_ENV=test vitest",
35+
"server": "node ./bin/json-graphql-server.cjs example/data.cjs",
36+
"prepublish": "yarn build",
37+
"build": "yarn build-ts && yarn build-esm-cjs && yarn build-node && yarn build-umd",
38+
"build-ts": "tsc",
39+
"build-esm-cjs": "vite build",
40+
"build-node": "vite build -c ./vite.config.node.js",
41+
"build-umd": "vite build -c ./vite.config.umd.js"
3642
},
3743
"lint-staged": {
38-
"src/**/*.js": [
39-
"eslint --fix",
40-
"git add"
44+
"*.{js,jsx,ts,tsx}": [
45+
"biome check --write"
4146
]
4247
},
4348
"devDependencies": {
44-
"@types/jest": "^29.5.12",
45-
"babel-eslint": "^10.0.3",
46-
"babel-jest": "^29.7.0",
47-
"eslint": "^9.0.0",
48-
"eslint-config-prettier": "^9.1.0",
49-
"eslint-plugin-import": "^2.29.1",
50-
"eslint-plugin-jest": "^28.2.0",
51-
"eslint-plugin-prettier": "^5.1.3",
52-
"husky": "^9.0.11",
53-
"jest": "^29.7.0",
54-
"lint-staged": "^15.2.2",
55-
"prettier": "^3.2.5",
56-
"supertest": "^6.3.4",
57-
"vite": "^5.4.12"
49+
"@biomejs/biome": "1.9.4",
50+
"@types/express": "^4.17.21",
51+
"cross-env": "^7.0.3",
52+
"husky": "^9.1.7",
53+
"lint-staged": "^15.4.3",
54+
"supertest": "^7.0.0",
55+
"typescript": "^5.7.3",
56+
"vite": "^6.1.0",
57+
"vitest": "^3.0.5"
5858
},
5959
"dependencies": {
60-
"@apollo/client": "^3.9.11",
61-
"@graphql-tools/schema": "^10.0.3",
60+
"@apollo/client": "^3.12.11",
61+
"@graphql-tools/schema": "^10.0.18",
6262
"cors": "^2.8.5",
63-
"express": "^4.20.0",
64-
"graphql": "^16.8.1",
65-
"graphql-http": "^1.22.1",
66-
"graphql-tag": "^2.12.6",
63+
"express": "^4.21.2",
64+
"graphql": "^16.10.0",
65+
"graphql-http": "^1.22.4",
6766
"graphql-type-json": "^0.3.2",
68-
"inflection": "^3.0.0",
67+
"inflection": "^3.0.2",
6968
"lodash.merge": "^4.6.2",
70-
"reify": "^0.20.12",
7169
"xhr-mock": "^2.5.1"
7270
},
7371
"bin": {

src/client.js src/client.ts

File renamed without changes.

src/createApolloClient.js

-13
This file was deleted.

src/createApolloClient.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ApolloClient, InMemoryCache } from '@apollo/client';
2+
import { SchemaLink } from '@apollo/client/link/schema';
3+
import getSchemaFromData from './introspection/getSchemaFromData';
4+
import type { Data } from './types';
5+
6+
export default (data: Data) => {
7+
const schema = getSchemaFromData(data);
8+
9+
const client = new ApolloClient({
10+
cache: new InMemoryCache(),
11+
link: new SchemaLink({ schema }),
12+
});
13+
14+
return client;
15+
};

src/global.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare global {
2+
interface Window {
3+
JsonGraphqlServer: object;
4+
jsonSchemaBuilder: object;
5+
}
6+
}
7+
export default global;

src/graphQLClientServer.js src/graphQLClientServer.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import mock, { proxy } from 'xhr-mock';
22
import handleRequestFactory from './handleRequest';
3+
import type { Data } from './types';
34

45
/**
56
* Starts a GraphQL Server in your browser: intercepts every call to http://localhost:3000/graphql
@@ -40,7 +41,7 @@ import handleRequestFactory from './handleRequest';
4041
* GraphQLClientServer(data);
4142
* GraphQLClientServer(data, 'http://localhost:8080/api/graphql');
4243
*/
43-
export default function ({ data, url }) {
44+
export default function ({ data, url }: { data: Data; url: string }) {
4445
const handleRequest = handleRequestFactory(data);
4546

4647
return {
@@ -62,7 +63,7 @@ export default function ({ data, url }) {
6263

6364
resolve(res);
6465
});
65-
})
66+
}),
6667
);
6768

6869
// Ensure all other requests are handled by the default XmlHttpRequest

src/graphiqlHandler.js src/graphiqlHandler.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
export const graphiqlHandler = (req, res) => {
1+
import type { Handler } from 'express';
2+
3+
export const graphiqlHandler: Handler = (_, res) => {
24
res.writeHead(200, undefined, {
35
'Content-Type': 'text/html; charset=utf-8',
46
});
57
return res.end(
68
getGraphiqlHtml({
79
endpoint: '/graphql',
8-
})
10+
}),
911
);
1012
};
1113

12-
const getGraphiqlHtml = ({ endpoint }) => `
14+
const getGraphiqlHtml = ({ endpoint }: { endpoint: string }) => `
1315
<!--
1416
* Copyright (c) 2021 GraphQL Contributors
1517
* All rights reserved.
@@ -82,4 +84,4 @@ const getGraphiqlHtml = ({ endpoint }) => `
8284
);
8385
</script>
8486
</body>
85-
</html>`;
87+
</html>`;

src/handleRequest.js src/handleRequest.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { graphql } from 'graphql';
22
import schemaBuilder from './schemaBuilder';
3+
import type { Data } from './types';
34

45
/**
56
* Starts a GraphQL Server in your browser: intercepts every call to http://localhost:3000/graphql
@@ -40,9 +41,9 @@ import schemaBuilder from './schemaBuilder';
4041
* GraphQLClientServer(data);
4142
* GraphQLClientServer(data, 'http://localhost:8080/api/graphql');
4243
*/
43-
export default function (data) {
44+
export default function (data: Data) {
4445
const schema = schemaBuilder(data);
45-
return (url, opts = {}) => {
46+
return (url: any, opts: any = {}) => {
4647
let body = opts.body;
4748

4849
if (url.requestBody) {
@@ -54,7 +55,7 @@ export default function (data) {
5455
return graphql({
5556
schema,
5657
source: query.query,
57-
variableValues: query.variables
58+
variableValues: query.variables,
5859
}).then(
5960
(result) => ({
6061
status: 200,
@@ -65,7 +66,7 @@ export default function (data) {
6566
status: 500,
6667
headers: { 'Content-Type': 'application/json' },
6768
body: JSON.stringify(error),
68-
})
69+
}),
6970
);
7071
};
7172
}

0 commit comments

Comments
 (0)