Skip to content

Commit 5f8f400

Browse files
committed
Merge branch 'master' into modernize
2 parents e7bb32b + 65bb0d1 commit 5f8f400

6 files changed

+388
-317
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ format: ## Format the source code
2222
@yarn run format
2323

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

2727
build: ## Build production release
2828
@yarn run build

README.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@ Get a full fake GraphQL API with zero coding in less than 30 seconds.
88
> I'd love to learn GraphQL, but it seems that I first have to read a book about GraphQL Types and Queries, then install a gazillion npm packages.
99
> - About every developer
1010
11-
Start playing with GraphQL right away with `json-graphql-server`, a testing and mocking tool for GraphQL. All it takes is a JSON of your data.
11+
Start playing with GraphQL right away with `json-graphql-server`, a testing and mocking tool for GraphQL written in Node.js. All it takes is a JSON of your data.
1212

1313
Inspired by the excellent [json-server](https://github.com/typicode/json-server).
1414

15-
## Example
16-
17-
Follow the guide below starting from scratch, or see the example live on StackBlitz:
15+
## Usage
1816

1917
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/fork/json-graphql-server)
2018

21-
22-
Create a `db.js` file.
23-
24-
Your data file should export an object where the keys are the entity types. The values should be lists of entities, i.e. arrays of value objects with at least an `id` key. For instance:
19+
Create a `db.js` file that exports an object where the keys are the entity types. The values should be lists of entities, i.e. arrays of value objects with at least an `id` key. For instance:
2520

2621
```js
2722
module.exports = {
@@ -40,14 +35,23 @@ module.exports = {
4035
}
4136
```
4237

43-
Start the GraphQL server on localhost, port 3000.
38+
Use the `npx json-graphql-server <db_file_name>` command to start the GraphQL server on localhost, port 3000.
4439

4540
```sh
4641
npx json-graphql-server db.js
4742
```
4843

49-
To use a port other than 3000, you can run `json-graphql-server db.js --p <your port here>`
50-
To use a host other than localhost, you can run `json-graphql-server db.js -h <your host here>` or `--host <your host here>`
44+
To use a different port, use the `--port` or `-p` option:
45+
46+
```
47+
npx json-graphql-server db.js --p 8080
48+
```
49+
50+
To use a different host, use the `--host` or `-h` option:
51+
52+
```
53+
npx json-graphql-server db.js -h 127.0.0.1
54+
```
5155

5256
Now you can query your data in graphql. For instance, to issue the following query:
5357

bin/json-graphql-server.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var express = require('express');
44
var cors = require('cors');
55
var { jsonGraphqlExpress } = require('../dist/json-graphql-server-node.cjs');
66
var dataFilePath = process.argv.length > 2 ? process.argv[2] : './data.json';
7-
var data = require(path.join(process.cwd(), dataFilePath));
7+
var data = require(path.resolve(process.cwd(), dataFilePath));
88
var PORT = process.env.NODE_PORT || 3000;
99
var HOST = process.env.NODE_HOST || 'localhost';
1010
var app = express();

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-graphql-server",
3-
"version": "3.1.0",
3+
"version": "3.1.2",
44
"type": "module",
55
"main": "./dist/json-graphql-server.cjs",
66
"module": "./dist/json-graphql-server.js",
@@ -53,14 +53,14 @@
5353
"lint-staged": "^15.2.2",
5454
"supertest": "^6.3.4",
5555
"typescript": "^5.5.4",
56-
"vite": "^5.2.8",
56+
"vite": "^5.4.12",
5757
"vitest": "^2.0.5"
5858
},
5959
"dependencies": {
6060
"@apollo/client": "^3.9.11",
6161
"@graphql-tools/schema": "^10.0.3",
6262
"cors": "^2.8.5",
63-
"express": "^4.17.3",
63+
"express": "^4.20.0",
6464
"graphql": "^16.8.1",
6565
"graphql-http": "^1.22.1",
6666
"graphql-type-json": "^0.3.2",
@@ -70,5 +70,6 @@
7070
},
7171
"bin": {
7272
"json-graphql-server": "bin/json-graphql-server.cjs"
73-
}
73+
},
74+
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
7475
}

src/graphiqlHandler.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export const graphiqlHandler: Handler = (_, res) => {
66
});
77
return res.end(
88
getGraphiqlHtml({
9-
endpoint: '/',
10-
}),
9+
endpoint: '/graphql',
10+
})
1111
);
1212
};
1313

@@ -73,7 +73,6 @@ const getGraphiqlHtml = ({ endpoint }: { endpoint: string }) => `
7373
const root = ReactDOM.createRoot(document.getElementById('graphiql'));
7474
const fetcher = GraphiQL.createFetcher({
7575
url: '${endpoint}',
76-
headers: { 'X-Example-Header': 'foo' },
7776
});
7877
const explorerPlugin = GraphiQLPluginExplorer.explorerPlugin();
7978
root.render(

0 commit comments

Comments
 (0)