🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →

json-to-graphql-query

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-to-graphql-query - npm Package Compare versions

Comparing version

to
1.3.0

@@ -29,28 +29,32 @@ "use strict";

}
function filterNonConfigFields(fieldName) {
return fieldName !== '__args' && fieldName !== '__alias';
}
function convertQuery(node, level, output) {
for (var key in node) {
if (key != '__args') {
if (typeof node[key] == 'object') {
var fieldCount = Object.keys(node[key]).length;
var token = void 0;
var subFields = void 0;
if (typeof node[key].__args == 'object') {
token = key + " (" + buildArgs(node[key].__args) + ")";
subFields = fieldCount > 1;
}
else {
token = "" + key;
subFields = fieldCount > 0;
}
output.push([token + (subFields ? ' {' : ''), level]);
convertQuery(node[key], level + 1, output);
if (subFields) {
output.push(['}', level]);
}
Object.keys(node)
.filter(filterNonConfigFields)
.forEach(function (key) {
if (typeof node[key] === 'object') {
var fieldCount = Object.keys(node[key]).filter(filterNonConfigFields).length;
var subFields = fieldCount > 0;
var token = void 0;
if (typeof node[key].__args === 'object') {
token = key + " (" + buildArgs(node[key].__args) + ")";
}
else {
output.push(["" + key, level]);
token = "" + key;
}
if (typeof node[key].__alias === 'string') {
token = node[key].__alias + ": " + token;
}
output.push([token + (fieldCount > 0 ? ' {' : ''), level]);
convertQuery(node[key], level + 1, output);
if (subFields) {
output.push(['}', level]);
}
}
}
else if (node[key]) {
output.push(["" + key, level]);
}
});
}

@@ -57,0 +61,0 @@ function jsonToGraphQLQuery(query, options) {

{
"name": "json-to-graphql-query",
"version": "1.2.0",
"version": "1.3.0",
"main": "lib/index.js",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -16,6 +16,7 @@ # json-to-graphql-query

* Converts a JavaScript object to a GraphQL Query
* Converts a JavaScript object to a GraphQL Query string
* Full support for nested query nodes and arguments
* Support for Enum values
* Supports JSON input types for arguments
* Support for aliases via `__alias`
* Support for input arguments via `__args`
* Support for Enum values via `EnumType`

@@ -131,2 +132,72 @@ See usage examples below :)

### Query with disabled fields
```typescript
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
id: true,
title: false,
comments: {
id: true,
comment: false,
user: true
}
},
User: false
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
```
Resulting `graphql_query`
```graphql
query {
Posts {
id
comments {
id
user
}
}
}
```
### Using aliases
```typescript
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
__alias: 'allPosts',
id: true,
comments: {
id: true,
comment: true
}
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
```
Resulting `graphql_query`
```graphql
query {
allPosts: Posts {
id
comments {
id
comment
}
}
}
```
## TO-DO List

@@ -133,0 +204,0 @@

Sorry, the diff of this file is not supported yet