Skip to content

Commit dcf47e5

Browse files
committed
More complete README
1 parent 80658ae commit dcf47e5

File tree

1 file changed

+74
-24
lines changed

1 file changed

+74
-24
lines changed

README.md

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,9 @@ Get a full fake GraphQL API with zero coding in less than 30 seconds.
99
1010
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.
1111

12-
## Usage
12+
## Example
1313

14-
* CLI
15-
16-
```sh
17-
npm install -g json-graphql-server
18-
json-graphql-server path/to/data.js
19-
```
20-
21-
* Node
22-
23-
```js
24-
import express from 'express';
25-
import { jsonGraphqlExpress } from 'json-graphql-server';
26-
27-
const PORT = 3000;
28-
const app = express();
29-
const data = {
30-
// ... your data
31-
};
32-
app.use('/graphql', jsonGraphqlExpress(data));
33-
app.listen(PORT);
34-
```
35-
36-
## Example Data File
14+
Create a `db.json` file.
3715

3816
Your data file should be an object where the keys are the entity types. The values should be lists of entities, i.e. arrays of value objects with at lead an `id` key. For instance:
3917

@@ -78,6 +56,48 @@ Your data file should be an object where the keys are the entity types. The valu
7856
}
7957
```
8058

59+
Start the GraphQL server on localhost, port 3000.
60+
61+
```sh
62+
json-graphql-server db.json
63+
```
64+
65+
Now you can query your data in graphql. For instance, to issue the following query:
66+
67+
```graphql
68+
query {
69+
Customer(id: 1) {
70+
id
71+
first_name
72+
last_name
73+
}
74+
}
75+
```
76+
77+
Go to http://localhost:3000/?query=query%20%7B%20Post(id%3A%201)%20%7Bid%20title%20views%20%7D%7D. You'll get the following result:
78+
79+
```json
80+
{
81+
"data": {
82+
"Post": {
83+
"id": "1",
84+
"title": "Lorem Ipsum",
85+
"views": 254,
86+
}
87+
}
88+
}
89+
```
90+
91+
The json-graphql-server accepts queries in GET and POST. Under the hood, it uses [Apollo's `graphql-server` module](http://dev.apollodata.com/tools/graphql-server/requests.html). Please refer to their documentations for details about passing variables, etc.
92+
93+
Note that the server is [GraphiQL](https://github.com/skevy/graphiql-app/releases) enabled.
94+
95+
## Install
96+
97+
```sh
98+
npm install -g json-graphql-server
99+
```
100+
81101
## Generated Types and Queries
82102

83103
Based on your data, json-graphql-server will generate a schema with one type per entity, as well as 3 query types and 3 mutation types. For instance for the `Post` entity:
@@ -172,6 +192,30 @@ Here is how you can use the queries and mutations generated for your data, using
172192
</tr>
173193
</table>
174194

195+
196+
## Usage with Node
197+
198+
Install the module locally
199+
200+
```sh
201+
npm install --save-dev json-graphql-server
202+
```
203+
204+
Then use the `jsonGraphqlExpress` express middleware:
205+
206+
```js
207+
import express from 'express';
208+
import { jsonGraphqlExpress } from 'json-graphql-server';
209+
210+
const PORT = 3000;
211+
const app = express();
212+
const data = {
213+
// ... your data
214+
};
215+
app.use('/graphql', jsonGraphqlExpress(data));
216+
app.listen(PORT);
217+
```
218+
175219
## Adding Authentication, Custom Routes, etc.
176220

177221
`json-graphql-server` doesn't deal with authentication or custom routes. But you can use your favorite middleware with Express:
@@ -196,6 +240,12 @@ app.listen(PORT);
196240

197241
Deploy with Heroku or Next.js.
198242

243+
## Roadmap
244+
245+
* Handle relationships
246+
* Client-side mocking (à la [FakeRest](https://github.com/marmelab/FakeRest))
247+
* CLI options (port, https, watch, delay, custom schema)
248+
199249
## Contributing
200250

201251
Use Prettier formatting and make sure you include unit tests. The project includes a `Makefile` to automate usual developer tasks:

0 commit comments

Comments
 (0)