Skip to content

Commit e06650a

Browse files
GH-2 Adding support for enum argument types in queries.
1 parent b161470 commit e06650a

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ query {
5656
### Query with arguments
5757

5858
```typescript
59-
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
59+
import { jsonToGraphQLQuery, EnumType } from 'json-to-graphql-query';
6060

6161
const query = {
6262
query: {
6363
Posts: {
6464
__args: {
6565
where: { id: 2 }
66-
orderBy: 'post_date'
66+
orderBy: 'post_date',
67+
status: new EnumType('PUBLISHED')
6768
},
6869
id: true,
6970
title: true,
@@ -78,7 +79,7 @@ Resulting `graphql_query`
7879

7980
```graphql
8081
query {
81-
Posts (where: {id: 2}, orderBy: "post_date") {
82+
Posts (where: {id: 2}, orderBy: "post_date", status: PUBLISHED) {
8283
id
8384
title
8485
post_date
@@ -132,4 +133,4 @@ Pull requests welcome!
132133

133134
## License
134135

135-
MIT
136+
MIT

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-to-graphql-query",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"main": "lib/index.js",
55
"license": "MIT",
66
"scripts": {

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { expect } from 'chai';
3-
import { jsonToGraphQLQuery } from '../';
3+
import { jsonToGraphQLQuery, EnumType } from '../';
44

55
describe('jsonToGraphQL()', () => {
66

@@ -66,6 +66,29 @@ describe('jsonToGraphQL()', () => {
6666
}`);
6767
});
6868

69+
it('converts a query with enum arguments', () => {
70+
const query = {
71+
query: {
72+
Posts: {
73+
__args: {
74+
status: new EnumType('PUBLISHED')
75+
},
76+
id: true,
77+
title: true,
78+
post_date: true
79+
}
80+
}
81+
};
82+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
83+
`query {
84+
Posts (status: PUBLISHED) {
85+
id
86+
title
87+
post_date
88+
}
89+
}`);
90+
});
91+
6992
it('converts a query with JSON arguments', () => {
7093
const query = {
7194
query: {
@@ -243,4 +266,4 @@ describe('jsonToGraphQL()', () => {
243266
'query { Posts (arg1: 20, arg2: "flibble") { id title } }'
244267
);
245268
});
246-
});
269+
});

src/jsonToGraphQLQuery.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
21
function stringify(obj_from_json: any): string {
2+
if (obj_from_json instanceof EnumType) {
3+
return obj_from_json.value;
4+
}
35
// Cheers to Derek: https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties
4-
if (typeof obj_from_json !== 'object' || obj_from_json === null) {
6+
else if (typeof obj_from_json !== 'object' || obj_from_json === null) {
57
// not an object, stringify using native function
68
return JSON.stringify(obj_from_json);
79
}
@@ -84,4 +86,8 @@ export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions =
8486
}
8587
});
8688
return output;
87-
}
89+
}
90+
91+
export class EnumType {
92+
constructor(public value: string) {}
93+
}

0 commit comments

Comments
 (0)