Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ query {
### Query with arguments

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

const query = {
query: {
Posts: {
__args: {
where: { id: 2 }
orderBy: 'post_date'
orderBy: 'post_date',
status: new EnumType('PUBLISHED')
},
id: true,
title: true,
Expand All @@ -78,7 +79,7 @@ Resulting `graphql_query`

```graphql
query {
Posts (where: {id: 2}, orderBy: "post_date") {
Posts (where: {id: 2}, orderBy: "post_date", status: PUBLISHED) {
id
title
post_date
Expand Down Expand Up @@ -132,4 +133,4 @@ Pull requests welcome!

## License

MIT
MIT
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-to-graphql-query",
"version": "1.1.2",
"version": "1.1.3",
"main": "lib/index.js",
"license": "MIT",
"scripts": {
Expand Down
27 changes: 25 additions & 2 deletions src/__tests__/jsonToGraphQLQuery.tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { expect } from 'chai';
import { jsonToGraphQLQuery } from '../';
import { jsonToGraphQLQuery, EnumType } from '../';

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

Expand Down Expand Up @@ -66,6 +66,29 @@ describe('jsonToGraphQL()', () => {
}`);
});

it('converts a query with enum arguments', () => {
const query = {
query: {
Posts: {
__args: {
status: new EnumType('PUBLISHED')
},
id: true,
title: true,
post_date: true
}
}
};
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
`query {
Posts (status: PUBLISHED) {
id
title
post_date
}
}`);
});

it('converts a query with JSON arguments', () => {
const query = {
query: {
Expand Down Expand Up @@ -243,4 +266,4 @@ describe('jsonToGraphQL()', () => {
'query { Posts (arg1: 20, arg2: "flibble") { id title } }'
);
});
});
});
12 changes: 9 additions & 3 deletions src/jsonToGraphQLQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

function stringify(obj_from_json: any): string {
if (obj_from_json instanceof EnumType) {
return obj_from_json.value;
}
// Cheers to Derek: https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties
if (typeof obj_from_json !== 'object' || obj_from_json === null) {
else if (typeof obj_from_json !== 'object' || obj_from_json === null) {
// not an object, stringify using native function
return JSON.stringify(obj_from_json);
}
Expand Down Expand Up @@ -84,4 +86,8 @@ export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions =
}
});
return output;
}
}

export class EnumType {
constructor(public value: string) {}
}