diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index f32a8ab..0000000 --- a/.eslintrc +++ /dev/null @@ -1,34 +0,0 @@ -{ - "parser": "babel-eslint", - "env": { - "browser": true, - "es6": true, - "mocha": true, - "node": true, - "jest/globals": true - }, - "extends": [ - "eslint:recommended", - "prettier", - "plugin:prettier/recommended", - "plugin:import/errors", - "plugin:import/warnings" - ], - "plugins": ["prettier", "jest"], - "rules": { - "prettier/prettier": ["error"], - "import/no-extraneous-dependencies": "off", - "no-console": [ - "error", - { - "allow": ["warn", "error"] - } - ], - "no-unused-vars": [ - "error", - { - "ignoreRestSiblings": true - } - ] - } -} diff --git a/Makefile b/Makefile index 86e9a48..44952e7 100644 --- a/Makefile +++ b/Makefile @@ -13,18 +13,16 @@ watch: ## continuously compile ES6 files to JS @yarn vite build --watch test: ## Launch unit tests - @NODE_ENV=test NODE_OPTIONS="$$NODE_OPTIONS --experimental-vm-modules" ./node_modules/.bin/jest + @yarn run test watch-test: ## Launch unit tests and watch for changes - @NODE_ENV=test NODE_OPTIONS="$$NODE_OPTIONS --experimental-vm-modules" ./node_modules/.bin/jest --watch + @yarn run watch-test format: ## Format the source code - @./node_modules/.bin/eslint --fix ./src + @yarn run format run: ## Launch server with example data - @node ./bin/json-graphql-server.js example/data.js + @yarn run server build: ## Build production release - @yarn vite build - @yarn vite build -c ./vite.config.node.js - @yarn vite build -c ./vite.config.umd.js + @yarn run build diff --git a/README.md b/README.md index de60632..518e9b5 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ module.exports = { Start the GraphQL server on localhost, port 3000. ```sh -json-graphql-server db.js +npx json-graphql-server db.js ``` To use a port other than 3000, you can run `json-graphql-server db.js --p ` @@ -98,7 +98,7 @@ Note that the server is [GraphiQL](https://github.com/graphql/graphiql) enabled, ## Install ```sh -npm install -g json-graphql-server +npm install -D json-graphql-server ``` ## Generated Types and Queries @@ -448,7 +448,7 @@ Then use the `jsonGraphqlExpress` express middleware: ```js import express from 'express'; -import jsonGraphqlExpress from 'json-graphql-server/node'; +import { jsonGraphqlExpress } from 'json-graphql-server/node'; const PORT = 3000; const app = express(); diff --git a/bin/json-graphql-server.cjs b/bin/json-graphql-server.cjs index 1437858..703209c 100755 --- a/bin/json-graphql-server.cjs +++ b/bin/json-graphql-server.cjs @@ -1,9 +1,8 @@ #!/usr/bin/env node -require('reify'); var path = require('path'); var express = require('express'); var cors = require('cors'); -var JsonGraphqlServer = require('../dist/json-graphql-server-node.cjs').default; +var { jsonGraphqlExpress } = require('../dist/json-graphql-server-node.cjs'); var dataFilePath = process.argv.length > 2 ? process.argv[2] : './data.json'; var data = require(path.join(process.cwd(), dataFilePath)); var PORT = process.env.NODE_PORT || 3000; @@ -22,7 +21,7 @@ process.argv.forEach((arg, index) => { }); app.use(cors()); -app.use('/', JsonGraphqlServer(data)); +app.use('/', jsonGraphqlExpress(data)); app.listen(PORT, HOST); var msg = `GraphQL server running with your data at http://${HOST}:${PORT}/`; console.log(msg); // eslint-disable-line no-console diff --git a/biome.json b/biome.json new file mode 100644 index 0000000..9a45182 --- /dev/null +++ b/biome.json @@ -0,0 +1,27 @@ +{ + "$schema": "https://biomejs.dev/schemas/1.7.0/schema.json", + "organizeImports": { + "enabled": true + }, + "formatter": { + "enabled": true, + "indentStyle": "space", + "indentWidth": 4, + "ignore": ["public/*.js"] + }, + "javascript": { + "formatter": { + "quoteStyle": "single" + } + }, + "linter": { + "enabled": true, + "rules": { + "recommended": true, + "suspicious": { + "noExplicitAny": "off" + } + }, + "ignore": ["public/*.js"] + } +} diff --git a/example/data.js b/example/data.cjs similarity index 100% rename from example/data.js rename to example/data.cjs diff --git a/package.json b/package.json index 087cd68..7356e2c 100644 --- a/package.json +++ b/package.json @@ -27,34 +27,34 @@ ], "license": "MIT", "scripts": { - "format": "make format", + "format": "biome format --write src", + "lint": "biome lint --apply src", "precommit": "lint-staged", - "test": "jest", - "watch-test": "make watch-test", - "server": "make run", - "prepublish": "make build" + "test": "cross-env NODE_ENV=test vitest", + "server": "node ./bin/json-graphql-server.js example/data.js", + "prepublish": "yarn build", + "build": "yarn build-ts && yarn build-esm-cjs && yarn build-node && yarn build-umd", + "build-ts": "tsc", + "build-esm-cjs": "vite build", + "build-node": "vite build -c ./vite.config.node.js", + "build-umd": "vite build -c ./vite.config.umd.js" }, "lint-staged": { - "src/**/*.js": [ - "eslint --fix", - "git add" + "*.{js,jsx,ts,tsx}": [ + "biome lint --apply", + "biome format --write" ] }, "devDependencies": { - "@types/jest": "^29.5.12", - "babel-eslint": "^10.0.3", - "babel-jest": "^29.7.0", - "eslint": "^9.0.0", - "eslint-config-prettier": "^9.1.0", - "eslint-plugin-import": "^2.29.1", - "eslint-plugin-jest": "^28.2.0", - "eslint-plugin-prettier": "^5.1.3", + "@biomejs/biome": "1.7.0", + "@types/express": "^4.17.21", + "cross-env": "^7.0.3", "husky": "^9.0.11", - "jest": "^29.7.0", "lint-staged": "^15.2.2", - "prettier": "^3.2.5", "supertest": "^6.3.4", - "vite": "^5.2.8" + "typescript": "^5.5.4", + "vite": "^5.2.8", + "vitest": "^2.0.5" }, "dependencies": { "@apollo/client": "^3.9.11", @@ -63,14 +63,12 @@ "express": "^4.17.3", "graphql": "^16.8.1", "graphql-http": "^1.22.1", - "graphql-tag": "^2.12.6", "graphql-type-json": "^0.3.2", "inflection": "^3.0.0", "lodash.merge": "^4.6.2", - "reify": "^0.20.12", "xhr-mock": "^2.5.1" }, "bin": { "json-graphql-server": "bin/json-graphql-server.cjs" } -} \ No newline at end of file +} diff --git a/src/client.js b/src/client.ts similarity index 100% rename from src/client.js rename to src/client.ts diff --git a/src/createApolloClient.js b/src/createApolloClient.js deleted file mode 100644 index b18dc24..0000000 --- a/src/createApolloClient.js +++ /dev/null @@ -1,13 +0,0 @@ -import { ApolloClient, mockNetworkInterfaceWithSchema } from '@apollo/client'; -import getSchemaFromData from './introspection/getSchemaFromData'; - -export default (data) => { - const schema = getSchemaFromData(data); - const mockNetworkInterface = mockNetworkInterfaceWithSchema({ schema }); - - const client = new ApolloClient({ - networkInterface: mockNetworkInterface, - }); - - return client; -}; diff --git a/src/createApolloClient.ts b/src/createApolloClient.ts new file mode 100644 index 0000000..f1627bc --- /dev/null +++ b/src/createApolloClient.ts @@ -0,0 +1,15 @@ +import { ApolloClient, InMemoryCache } from '@apollo/client'; +import { SchemaLink } from '@apollo/client/link/schema'; +import getSchemaFromData from './introspection/getSchemaFromData'; +import type { Data } from './types'; + +export default (data: Data) => { + const schema = getSchemaFromData(data); + + const client = new ApolloClient({ + cache: new InMemoryCache(), + link: new SchemaLink({ schema }), + }); + + return client; +}; diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 0000000..c769513 --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1,7 @@ +declare global { + interface Window { + JsonGraphqlServer: object; + jsonSchemaBuilder: object; + } +} +export default global; diff --git a/src/graphQLClientServer.js b/src/graphQLClientServer.ts similarity index 94% rename from src/graphQLClientServer.js rename to src/graphQLClientServer.ts index bbe07cf..d309029 100644 --- a/src/graphQLClientServer.js +++ b/src/graphQLClientServer.ts @@ -1,5 +1,6 @@ import mock, { proxy } from 'xhr-mock'; import handleRequestFactory from './handleRequest'; +import type { Data } from './types'; /** * Starts a GraphQL Server in your browser: intercepts every call to http://localhost:3000/graphql @@ -40,7 +41,7 @@ import handleRequestFactory from './handleRequest'; * GraphQLClientServer(data); * GraphQLClientServer(data, 'http://localhost:8080/api/graphql'); */ -export default function ({ data, url }) { +export default function ({ data, url }: { data: Data; url: string }) { const handleRequest = handleRequestFactory(data); return { @@ -62,7 +63,7 @@ export default function ({ data, url }) { resolve(res); }); - }) + }), ); // Ensure all other requests are handled by the default XmlHttpRequest diff --git a/src/graphiqlHandler.js b/src/graphiqlHandler.ts similarity index 92% rename from src/graphiqlHandler.js rename to src/graphiqlHandler.ts index fc59751..320e4fb 100644 --- a/src/graphiqlHandler.js +++ b/src/graphiqlHandler.ts @@ -1,15 +1,17 @@ -export const graphiqlHandler = (req, res) => { +import type { Handler } from 'express'; + +export const graphiqlHandler: Handler = (_, res) => { res.writeHead(200, undefined, { 'Content-Type': 'text/html; charset=utf-8', }); return res.end( getGraphiqlHtml({ endpoint: '/', - }) + }), ); }; -const getGraphiqlHtml = ({ endpoint }) => ` +const getGraphiqlHtml = ({ endpoint }: { endpoint: string }) => `