Skip to content

Commit f193186

Browse files
committed
initial commit
0 parents  commit f193186

File tree

10 files changed

+258
-0
lines changed

10 files changed

+258
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
lib
3+
*.lock
4+
*.log
5+
*.tgz
6+
package-lock.json

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.vscode
2+
node_modules
3+
src
4+
__tests__
5+
example
6+
tsconfig.json
7+
*.tgz

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Copyright 2017 Russell Briggs
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# json-to-graphql
2+
3+
This is a simple module that takes a JavaScript object and turns it into a
4+
GraphQL query to be sent to a GraphQL server.
5+
6+
Mainly useful for applications that need to generate graphql queries dynamically.
7+
8+
## Installation
9+
10+
```
11+
npm install json-to-graphql
12+
```
13+
14+
## Usage
15+
16+
### Simple Query
17+
18+
```typescript
19+
import { jsonToGraphQL } from 'json-to-graphql';
20+
21+
const query = {
22+
query: {
23+
Posts: {
24+
id: true,
25+
title: true,
26+
post_date: true
27+
}
28+
}
29+
}
30+
const graphql_query = jsonToGraphQL(query);
31+
```
32+
33+
Resulting `graphql_query`
34+
35+
```graphql
36+
query {
37+
Posts {
38+
id,
39+
title,
40+
post_date
41+
}
42+
}
43+
```
44+
45+
### Query with arguments
46+
47+
```typescript
48+
import { jsonToGraphQL } from 'json-to-graphql';
49+
50+
const query = {
51+
query: {
52+
Posts: {
53+
__args: {
54+
orderBy: 'post_date',
55+
userId: 12
56+
}
57+
id: true,
58+
title: true,
59+
post_date: true
60+
}
61+
}
62+
}
63+
const graphql_query = jsonToGraphQL(query);
64+
```
65+
66+
Resulting `graphql_query`
67+
68+
```graphql
69+
query {
70+
Posts (orderBy: "post_date", userId: 12) {
71+
id,
72+
title,
73+
post_date
74+
}
75+
}
76+
```
77+
78+
### Query with nested objects
79+
80+
```typescript
81+
import { jsonToGraphQL } from 'json-to-graphql';
82+
83+
const query = {
84+
query: {
85+
Posts: {
86+
id: true,
87+
title: true,
88+
comments: {
89+
id: true,
90+
comment: true,
91+
user: true
92+
}
93+
}
94+
}
95+
}
96+
const graphql_query = jsonToGraphQL(query);
97+
```
98+
99+
Resulting `graphql_query`
100+
101+
```graphql
102+
query {
103+
Posts (orderBy: "post_date", userId: 12) {
104+
id,
105+
name,
106+
post_date,
107+
comments {
108+
id,
109+
comment,
110+
user
111+
}
112+
}
113+
}
114+
```
115+
116+
## TO-DO List
117+
118+
* Fragments
119+
* Probably some other things!...
120+
121+
Pull requests welcome!
122+
123+
## License
124+
125+
MIT

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "json-to-graphql",
3+
"version": "0.0.1",
4+
"main": "lib/index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"build": "tslint -p . && tsc",
8+
"test": "mocha -r ts-node/register --recursive \"./src/**/__tests__/*\""
9+
},
10+
"typings": "lib/index.d.ts",
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/dupski/json-to-graphql.git"
14+
},
15+
"devDependencies": {
16+
"@types/chai": "^4.0.10",
17+
"@types/mocha": "^2.2.45",
18+
"chai": "^4.1.2",
19+
"mocha": "^4.1.0",
20+
"sinon": "^4.1.3",
21+
"ts-node": "^4.1.0",
22+
"tslint": "5.8.0",
23+
"typescript": "2.6.2"
24+
}
25+
}

src/__tests__/jsonToGraphQL.tests.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import { expect } from 'chai';
3+
import { jsonToGraphQL } from '../jsonToGraphQL';
4+
5+
describe('jsonToGraphQL()', () => {
6+
7+
it('throws if no query object is specified', () => {
8+
expect(() => {
9+
(jsonToGraphQL as any)();
10+
}).to.throw('query object not specified');
11+
});
12+
13+
it('throws if query is not an object', () => {
14+
expect(() => {
15+
jsonToGraphQL('not a query object');
16+
}).to.throw('query object not specified');
17+
});
18+
19+
});

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
export * from './jsonToGraphQL';

src/jsonToGraphQL.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
export function jsonToGraphQL(query: any) {
3+
if (!query || typeof query != 'object') {
4+
throw new Error('query object not specified');
5+
}
6+
}

tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"noImplicitAny": true,
6+
"outDir": "./lib",
7+
"declaration": true,
8+
"sourceMap": true,
9+
"experimentalDecorators": true,
10+
"removeComments": true,
11+
"lib": [
12+
"es2015"
13+
]
14+
},
15+
"include": [
16+
"./src/**/*"
17+
]
18+
}

tslint.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"extends": [
3+
"tslint:latest"
4+
],
5+
"rules": {
6+
"trailing-comma": [false],
7+
"object-literal-sort-keys": false,
8+
"ordered-imports": false,
9+
"no-console": [false],
10+
"no-unused-new": false,
11+
"triple-equals": false,
12+
"one-line": [false, "check-catch", "check-finally", "check-else"],
13+
"only-arrow-functions": [false],
14+
"quotemark": [true, "single"],
15+
"forin": false,
16+
"variable-name": [false],
17+
"no-empty": false,
18+
"no-implicit-dependencies": false,
19+
"no-string-literal": false,
20+
"no-submodule-imports": false,
21+
"object-literal-shorthand": false,
22+
"member-access": [false],
23+
"prefer-conditional-expression": false,
24+
"prefer-for-of": false,
25+
"member-ordering": [false],
26+
"prefer-object-spread": false,
27+
"eofline": false
28+
}
29+
}
30+

0 commit comments

Comments
 (0)