Skip to content

Commit 2862249

Browse files
authored
Merge pull request #2 from marmelab/readme
[WIP] Add README file
2 parents a020ba3 + b43b516 commit 2862249

File tree

1 file changed

+216
-1
lines changed

1 file changed

+216
-1
lines changed

README.md

Lines changed: 216 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,216 @@
1-
# FakeGraphQL
1+
# json-graphql-server
2+
3+
Get a full fake GraphQL API with zero coding in less than 30 seconds.
4+
5+
## Motivation
6+
7+
> 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.
8+
> - About every developer
9+
10+
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+
12+
## Usage
13+
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
37+
38+
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:
39+
40+
```json
41+
{
42+
"posts": [
43+
{
44+
"id": 1,
45+
"title": "Lorem Ipsum",
46+
"views": 254,
47+
"user_id": 123,
48+
"tag_id": "foo"
49+
},
50+
{
51+
"id": 2,
52+
"title": "Sic Dolor amet",
53+
"views": 65,
54+
"user_id": 456,
55+
"tag_id": "bar"
56+
},
57+
],
58+
"users": [
59+
{
60+
"id": 123,
61+
"name": "John Doe"
62+
},
63+
{
64+
"id": 456,
65+
"name": "Jane Doe"
66+
}
67+
],
68+
"tags": [
69+
{
70+
"id": "foo",
71+
"name": "Foo"
72+
},
73+
{
74+
"id": "bar",
75+
"name": "Bar"
76+
}
77+
]
78+
}
79+
```
80+
81+
## Generated Types and Queries
82+
83+
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:
84+
85+
```graphql
86+
type Post {
87+
id: ID!
88+
title: String!
89+
views: Int
90+
user_id: ID
91+
tag_id: ID
92+
}
93+
type Query {
94+
Post(id: ID!): Post
95+
allPosts(page: Int, perPage: Int, sortField: String, sortOrder: String, filter: String): [Customer]
96+
_allPostsMeta(page: Int, perPage: Int, sortField: String, sortOrder: String, filter: String): ListMetadata
97+
}
98+
type Mutation {
99+
createPost(data: String): Post
100+
updatePost(data: String): Post
101+
removePost(id: ID!): Boolean
102+
}
103+
type ListMetadata {
104+
count: Int!
105+
}
106+
```
107+
108+
## GraphQL Usage
109+
110+
Here is how you can use the queries and mutations generated for your data, using `Post` as an example:
111+
112+
<table>
113+
<tr>
114+
<th>Query / Mutation</th>
115+
<th>Result</th>
116+
</tr>
117+
<tr>
118+
<td>
119+
<pre>
120+
// get a list of entities for a type
121+
{
122+
allPosts {
123+
title
124+
views
125+
}
126+
}
127+
</pre>
128+
</td>
129+
<td>
130+
<pre>
131+
{
132+
"data": {
133+
"allPosts": [
134+
{ "title": "Lorem Ipsum", views: 254 },
135+
{ "title": "Sic Dolor amet", views: 65 }
136+
]
137+
}
138+
}
139+
</pre>
140+
</td>
141+
</tr>
142+
<tr>
143+
<td>
144+
<pre>
145+
// get a single entity, by id
146+
{
147+
Post(id: 1) {
148+
id
149+
title
150+
views
151+
user_id
152+
tag_id
153+
}
154+
}
155+
</pre>
156+
</td>
157+
<td>
158+
<pre>
159+
{
160+
"data": {
161+
"Post": {
162+
"id": 1,
163+
"title": "Lorem Ipsum",
164+
"views": 254,
165+
"user_id": 123,
166+
"tag_id": "foo"
167+
}
168+
}
169+
}
170+
</pre>
171+
</td>
172+
</tr>
173+
</table>
174+
175+
## Adding Authentication, Custom Routes, etc.
176+
177+
`json-graphql-server` doesn't deal with authentication or custom routes. But you can use your favorite middleware with Express:
178+
179+
```js
180+
import express from 'express';
181+
import { jsonGraphqlExpress } from 'json-graphql-server';
182+
183+
import OAuthSecurityMiddleWare from './path/to/OAuthSecurityMiddleWare';
184+
185+
const PORT = 3000;
186+
const app = express();
187+
const data = {
188+
// ... your data
189+
};
190+
app.use(OAuthSecurityMiddleWare());
191+
app.use('/graphql', jsonGraphqlExpress(data));
192+
app.listen(PORT);
193+
```
194+
195+
## Deployment
196+
197+
Deploy with Heroku or Next.js.
198+
199+
## Contributing
200+
201+
Use Prettier formatting and make sure you include unit tests. The project includes a `Makefile` to automate usual developer tasks:
202+
203+
```sh
204+
make install
205+
make build
206+
make test
207+
make watch
208+
make format
209+
```
210+
211+
## License
212+
213+
Admin-on-rest is licensed under the [MIT Licence](https://github.com/marmelab/json-graphql-server/blob/master/LICENSE.md), sponsored and supported by [marmelab](http://marmelab.com).
214+
215+
216+

0 commit comments

Comments
 (0)