json-to-graphql-query
Advanced tools
Comparing version
# json-to-graphql-query Changelog | ||
## 1.5.0 | ||
* Added `ignoreFields` option. Thanks @plmercereau | ||
## 1.4.0 | ||
@@ -5,0 +9,0 @@ |
@@ -0,4 +1,6 @@ | ||
export declare const configFields: string[]; | ||
export interface IJsonToGraphQLOptions { | ||
pretty?: boolean; | ||
ignoreFields?: string[]; | ||
} | ||
export declare function jsonToGraphQLQuery(query: any, options?: IJsonToGraphQLOptions): string; |
@@ -5,2 +5,3 @@ "use strict"; | ||
var VariableType_1 = require("./types/VariableType"); | ||
exports.configFields = ['__args', '__alias', '__variables', '__directives']; | ||
function stringify(obj_from_json) { | ||
@@ -42,11 +43,12 @@ if (obj_from_json instanceof EnumType_1.EnumType) { | ||
} | ||
function filterNonConfigFields(fieldName) { | ||
return fieldName !== '__args' && fieldName !== '__alias' && fieldName !== '__variables'; | ||
function filterNonConfigFields(fieldName, ignoreFields) { | ||
return exports.configFields.indexOf(fieldName) == -1 && ignoreFields.indexOf(fieldName) == -1; | ||
} | ||
function convertQuery(node, level, output) { | ||
function convertQuery(node, level, output, options) { | ||
Object.keys(node) | ||
.filter(filterNonConfigFields) | ||
.filter(function (key) { return filterNonConfigFields(key, options.ignoreFields); }) | ||
.forEach(function (key) { | ||
if (typeof node[key] === 'object') { | ||
var fieldCount = Object.keys(node[key]).filter(filterNonConfigFields).length; | ||
var fieldCount = Object.keys(node[key]) | ||
.filter(function (keyCount) { return filterNonConfigFields(keyCount, options.ignoreFields); }).length; | ||
var subFields = fieldCount > 0; | ||
@@ -67,3 +69,3 @@ var token = void 0; | ||
output.push([token + (fieldCount > 0 ? ' {' : ''), level]); | ||
convertQuery(node[key], level + 1, output); | ||
convertQuery(node[key], level + 1, output, options); | ||
if (subFields) { | ||
@@ -86,4 +88,7 @@ output.push(['}', level]); | ||
} | ||
if (!(options.ignoreFields instanceof Array)) { | ||
options.ignoreFields = []; | ||
} | ||
var queryLines = []; | ||
convertQuery(query, 0, queryLines); | ||
convertQuery(query, 0, queryLines, options); | ||
var output = ''; | ||
@@ -90,0 +95,0 @@ queryLines.forEach(function (_a) { |
{ | ||
"name": "json-to-graphql-query", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -14,2 +14,12 @@ # json-to-graphql-query | ||
## Usage | ||
```ts | ||
const query = jsonToGraphQLQuery(queryObject: object, options?: object); | ||
``` | ||
Supported Options: | ||
* `pretty` - boolean - optional - set to `true` to enable pretty-printed output | ||
* `ignoreFields` - string[] - optional - you can pass an array of object key names that you want removed from the query | ||
## Features | ||
@@ -19,2 +29,3 @@ | ||
* Full support for nested query / mutation nodes and arguments | ||
* Optionally strip specific object keys using the `ignoreFields` option | ||
* Support for input arguments via [`__args`](#query-with-arguments) | ||
@@ -29,9 +40,2 @@ * Support for query aliases via [`__alias`](#using-aliases) | ||
## Usage | ||
**jsonToGraphQLQuery(** queryObject: object, options?: object **)** | ||
Supported options: | ||
* **pretty**: boolean - Set to `true` to enable pretty-printed output | ||
### Simple Query | ||
@@ -66,3 +70,3 @@ | ||
### Query with arguments | ||
### Query with arguments | ||
@@ -207,3 +211,3 @@ ```typescript | ||
### Query with Enum Values | ||
### Query with Enum Values | ||
@@ -239,3 +243,3 @@ ```typescript | ||
### Query with variables | ||
### Query with variables | ||
@@ -275,2 +279,40 @@ ```typescript | ||
### Ignoring fields in the query object | ||
We sometimes want to ignore specific fields in the initial object, for instance __typename in Apollo queries. | ||
You can specify these fields using the `ignoreFields` option: | ||
```typescript | ||
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query'; | ||
const query = { | ||
query: { | ||
Posts: { | ||
shouldBeIgnored: { | ||
variable1: 'a value' | ||
}, | ||
id: true, | ||
title: true, | ||
post_date: true | ||
} | ||
} | ||
}; | ||
const graphql_query = jsonToGraphQLQuery(query, { | ||
pretty: true, | ||
ignoreFields: ['shouldBeIgnored'] | ||
}); | ||
``` | ||
Resulting `graphql_query` | ||
```graphql | ||
query { | ||
Posts { | ||
id | ||
title | ||
post_date | ||
} | ||
} | ||
``` | ||
## TO-DO List | ||
@@ -277,0 +319,0 @@ |
Sorry, the diff of this file is not supported yet
19640
10.53%184
3.95%321
15.05%