diff --git a/README.md b/README.md index e0f28d1..c639c13 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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 @@ -132,4 +133,4 @@ Pull requests welcome! ## License -MIT \ No newline at end of file +MIT diff --git a/package.json b/package.json index db7e50e..c418e8c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-to-graphql-query", - "version": "1.1.2", + "version": "1.1.3", "main": "lib/index.js", "license": "MIT", "scripts": { diff --git a/src/__tests__/jsonToGraphQLQuery.tests.ts b/src/__tests__/jsonToGraphQLQuery.tests.ts index d787f22..ee499ef 100644 --- a/src/__tests__/jsonToGraphQLQuery.tests.ts +++ b/src/__tests__/jsonToGraphQLQuery.tests.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; -import { jsonToGraphQLQuery } from '../'; +import { jsonToGraphQLQuery, EnumType } from '../'; describe('jsonToGraphQL()', () => { @@ -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: { @@ -243,4 +266,4 @@ describe('jsonToGraphQL()', () => { 'query { Posts (arg1: 20, arg2: "flibble") { id title } }' ); }); -}); \ No newline at end of file +}); diff --git a/src/jsonToGraphQLQuery.ts b/src/jsonToGraphQLQuery.ts index bc74757..a105c34 100644 --- a/src/jsonToGraphQLQuery.ts +++ b/src/jsonToGraphQLQuery.ts @@ -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); } @@ -84,4 +86,8 @@ export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions = } }); return output; -} \ No newline at end of file +} + +export class EnumType { + constructor(public value: string) {} +}