Skip to content

Commit e7bb32b

Browse files
committed
Modernize
1 parent 108a88f commit e7bb32b

Some content is hidden

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

70 files changed

+1192
-4281
lines changed

.eslintrc

-34
This file was deleted.

Makefile

+5-7
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ 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
2020

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

2424
run: ## Launch server with example data
25-
@node ./bin/json-graphql-server.js example/data.js
25+
@yarn run server
2626

2727
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
28+
@yarn run build

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = {
4343
Start the GraphQL server on localhost, port 3000.
4444

4545
```sh
46-
json-graphql-server db.js
46+
npx json-graphql-server db.js
4747
```
4848

4949
To use a port other than 3000, you can run `json-graphql-server db.js --p <your port here>`
@@ -98,7 +98,7 @@ Note that the server is [GraphiQL](https://github.com/graphql/graphiql) enabled,
9898
## Install
9999

100100
```sh
101-
npm install -g json-graphql-server
101+
npm install -D json-graphql-server
102102
```
103103

104104
## Generated Types and Queries
@@ -448,7 +448,7 @@ Then use the `jsonGraphqlExpress` express middleware:
448448

449449
```js
450450
import express from 'express';
451-
import jsonGraphqlExpress from 'json-graphql-server/node';
451+
import { jsonGraphqlExpress } from 'json-graphql-server/node';
452452

453453
const PORT = 3000;
454454
const app = express();

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.join(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/data.js example/data.cjs

File renamed without changes.

package.json

+20-22
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,34 @@
2727
],
2828
"license": "MIT",
2929
"scripts": {
30-
"format": "make format",
30+
"format": "biome format --write src",
31+
"lint": "biome lint --apply src",
3132
"precommit": "lint-staged",
32-
"test": "jest",
33-
"watch-test": "make watch-test",
34-
"server": "make run",
35-
"prepublish": "make build"
33+
"test": "cross-env NODE_ENV=test vitest",
34+
"server": "node ./bin/json-graphql-server.js example/data.js",
35+
"prepublish": "yarn build",
36+
"build": "yarn build-ts && yarn build-esm-cjs && yarn build-node && yarn build-umd",
37+
"build-ts": "tsc",
38+
"build-esm-cjs": "vite build",
39+
"build-node": "vite build -c ./vite.config.node.js",
40+
"build-umd": "vite build -c ./vite.config.umd.js"
3641
},
3742
"lint-staged": {
38-
"src/**/*.js": [
39-
"eslint --fix",
40-
"git add"
43+
"*.{js,jsx,ts,tsx}": [
44+
"biome lint --apply",
45+
"biome format --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",
49+
"@biomejs/biome": "1.7.0",
50+
"@types/express": "^4.17.21",
51+
"cross-env": "^7.0.3",
5252
"husky": "^9.0.11",
53-
"jest": "^29.7.0",
5453
"lint-staged": "^15.2.2",
55-
"prettier": "^3.2.5",
5654
"supertest": "^6.3.4",
57-
"vite": "^5.2.8"
55+
"typescript": "^5.5.4",
56+
"vite": "^5.2.8",
57+
"vitest": "^2.0.5"
5858
},
5959
"dependencies": {
6060
"@apollo/client": "^3.9.11",
@@ -63,14 +63,12 @@
6363
"express": "^4.17.3",
6464
"graphql": "^16.8.1",
6565
"graphql-http": "^1.22.1",
66-
"graphql-tag": "^2.12.6",
6766
"graphql-type-json": "^0.3.2",
6867
"inflection": "^3.0.0",
6968
"lodash.merge": "^4.6.2",
70-
"reify": "^0.20.12",
7169
"xhr-mock": "^2.5.1"
7270
},
7371
"bin": {
7472
"json-graphql-server": "bin/json-graphql-server.cjs"
7573
}
76-
}
74+
}

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: '/',
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.
@@ -83,4 +85,4 @@ const getGraphiqlHtml = ({ endpoint }) => `
8385
);
8486
</script>
8587
</body>
86-
</html>`;
88+
</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
}

src/introspection/DateType.js src/introspection/DateType.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Kind } from 'graphql/language';
33

44
const ISO_DATE_STRING_PATTERN = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/;
55

6-
export const isISODateString = (value) => {
6+
export const isISODateString = (value: any) => {
77
if (typeof value !== 'string') return false;
88
if (!ISO_DATE_STRING_PATTERN.test(value)) return false;
99
const d = new Date(value);
@@ -15,11 +15,11 @@ export const GraphQLDate = 'Date';
1515
export default new GraphQLScalarType({
1616
name: GraphQLDate,
1717
description: 'Date type',
18-
parseValue(value) {
18+
parseValue(value: any) {
1919
// value comes from the client
2020
return new Date(value); // sent to resolvers
2121
},
22-
serialize(value) {
22+
serialize(value: any) {
2323
// value comes from resolvers
2424
if (isISODateString(value)) return value;
2525
return value.toISOString(); // sent to the client
@@ -30,11 +30,15 @@ export default new GraphQLScalarType({
3030
if (ast.kind !== Kind.STRING) {
3131
throw new GraphQLError(
3232
`Query error: Can only parse dates strings, got a: ${ast.kind}`,
33-
[ast]
33+
{
34+
nodes: [ast],
35+
},
3436
);
3537
}
36-
if (isNaN(Date.parse(ast.value))) {
37-
throw new GraphQLError(`Query error: not a valid date`, [ast]);
38+
if (Number.isNaN(Date.parse(ast.value))) {
39+
throw new GraphQLError('Query error: not a valid date', {
40+
nodes: [ast],
41+
});
3842
}
3943
return new Date(ast.value);
4044
},

src/introspection/getFieldsFromEntities.spec.js src/introspection/getFieldsFromEntities.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test('does infer field types', () => {
77
{ id: 1, foo: 'foo1' },
88
{ id: 2, foo: 'foo2', bar: 'bar1' },
99
{ id: 3, bar: 'bar2' },
10-
])
10+
]),
1111
).toEqual({
1212
id: { type: new GraphQLNonNull(GraphQLID) },
1313
foo: { type: GraphQLString },

0 commit comments

Comments
 (0)