Skip to content

Commit 8568a51

Browse files
committed
config, readme and working example, and faker typings in config
1 parent b4efd1d commit 8568a51

File tree

21 files changed

+187
-26
lines changed

21 files changed

+187
-26
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,47 @@ Just run the CLI on your schema.
2525
demeter schema.graphql
2626
```
2727

28+
Mock server should be running on port 4000
29+
30+
## Configuration
31+
Demeter will automatically create config file on the first start `.graphql.demeter.js`.
32+
33+
![](./readme.png)
34+
35+
As you can see if you provide type it will autocomplete faker values
36+
37+
```js
38+
/** @type {import('graphql-demeter-core').FakerConfig} */
39+
const config = {
40+
objects: {
41+
Card:{
42+
description:{
43+
type:"values",
44+
values:["Very powerful card", "Most fire resistant character", "Good melee fighter"]
45+
},
46+
name:{
47+
type:"values",
48+
values:["Zeus", "Athena", "Hera", "Ares", "Kronos"]
49+
},
50+
image:{
51+
type:"faker",
52+
key:"internet.avatar"
53+
}
54+
}
55+
},
56+
scalars: {},
57+
};
58+
module.exports = config
59+
```
2860

2961
## Development & Examples
3062

3163
1. Clone this repository
3264
2. Install dependencies
3365
3. Run npm run run-example
3466

35-
You should have your server running on port 4000 with sample schema. Feel free to modify schema locally to just test it with your schema.
67+
You should have your server running on port 4000 with sample schema. Feel free to modify schema locally to just test it with your schema.
68+
69+
## Roadmap
70+
[ ] - reload on config change
71+
[ ] - add ai possibilities with OpenAI Key

examples/olympus/.graphql.demeter.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** @type {import('graphql-demeter-core').FakerConfig} */
2+
const config = {
3+
objects: {
4+
Card:{
5+
description:{
6+
type:"values",
7+
values:["Very powerful card", "Most fire resistant character", "Good melee fighter"]
8+
},
9+
name:{
10+
type:"values",
11+
values:["Zeus", "Athena", "Hera", "Ares", "Kronos"]
12+
},
13+
image:{
14+
type:"faker",
15+
key:"internet.avatar"
16+
}
17+
}
18+
},
19+
scalars: {},
20+
};
21+
module.exports = config

examples/olympus/example.query.gql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
query Draw{
2+
drawCard{
3+
name
4+
description
5+
Attack
6+
Defense
7+
}
8+
}

examples/olympus/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"private": true,
66
"main": "index.js",
77
"scripts": {
8-
"start": "demeter zeus.graphql"
8+
"start": "node ../../node_modules/.bin/demeter zeus.graphql"
99
},
1010
"author": "Aexol <aexol@aexol.com> (http://aexol.com)",
1111
"license": "ISC",

examples/olympus/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": false,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"incremental": true,
11+
"esModuleInterop": true,
12+
"module": "esnext",
13+
"moduleResolution": "node",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"jsx": "preserve",
17+
},
18+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".graphql.demeter.js"],
19+
"exclude": ["node_modules"]
20+
}
21+

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
"url": "https://github.com/graphql-editor/graphql-demeter.git"
1515
},
1616
"scripts": {
17-
"build": "tspc",
17+
"clean-packages":"rimraf packages/**/lib && rimraf packages/**/tsconfig.tsbuildinfo",
18+
"build": "npm run clean-packages && npm run build --ws --if-present",
1819
"lint": "tspc && eslint \"./packages/**/src/**/*.{ts,js}\" --quiet --fix",
1920
"cli": "node ./packages/graphql-demeter/lib/index.js",
20-
"run-example":"npm run build && npm run start -w olympus",
21+
"example": "npm run start -w olympus",
22+
"run-example": "npm run build && npm run example",
2123
"test": "jest"
2224
},
2325
"devDependencies": {
@@ -32,6 +34,7 @@
3234
"husky": "^8.0.3",
3335
"jest": "^29.7.0",
3436
"prettier": "^3.1.1",
37+
"rimraf": "^5.0.5",
3538
"ts-jest": "^29.1.1",
3639
"ts-node": "^10.9.2",
3740
"ts-patch": "^3.1.1",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"extends": ["../../.eslintrc"]
2+
"extends": ["../../.eslintrc"],
3+
"ignorePatterns":["lib"]
34
}

packages/graphql-demeter-core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "./lib/index.js",
66
"author": "GraphQL Editor, Artur Czemiel",
77
"scripts": {
8-
"build": "tspc --build tsconfig.build.json",
9-
"start": "tspc --build tsconfig.build.json --watch"
8+
"build": "tspc",
9+
"start": "tspc --watch"
1010
},
1111
"repository": {
1212
"type": "git",

packages/graphql-demeter-core/src/MockServer/models.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
1-
export type FakerConfiguratedField = {
2-
type: string;
3-
values?: Array<string | number>;
4-
};
1+
import type { faker } from '@faker-js/faker';
2+
3+
type FakerType = typeof faker;
4+
type FakerFunctionKey =
5+
| 'address'
6+
| 'commerce'
7+
| 'company'
8+
| 'database'
9+
| 'date'
10+
| 'finance'
11+
| 'hacker'
12+
| 'helpers'
13+
| 'internet'
14+
| 'lorem'
15+
| 'name'
16+
| 'phone'
17+
| 'random'
18+
| 'system';
19+
20+
type Join<Key, Previous, TKey extends number | string = string> = Key extends TKey
21+
? Previous extends TKey
22+
? `${Key}${'.'}${Previous}`
23+
: never
24+
: never;
25+
26+
type Previous = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...0[]];
27+
28+
export type Paths<TEntity, TDepth extends number = 3, TKey extends number | string = string> = [TDepth] extends [never]
29+
? never
30+
: TEntity extends object
31+
? {
32+
[Key in keyof TEntity]-?: Key extends TKey
33+
? `${Key}` | Join<Key, Paths<TEntity[Key], Previous[TDepth]>>
34+
: never;
35+
}[keyof TEntity]
36+
: '';
37+
38+
type AllowedFakerStrings = Exclude<Paths<Pick<FakerType, FakerFunctionKey>>, FakerFunctionKey>;
39+
40+
export type FakerConfiguratedField =
41+
| {
42+
type: 'values';
43+
values: Array<string | number>;
44+
}
45+
| {
46+
type: 'faker';
47+
key: AllowedFakerStrings;
48+
};
549

650
export type FakerConfig = {
751
objects: Record<string, Record<string, FakerConfiguratedField>> | undefined;

0 commit comments

Comments
 (0)