@walmartlabs/json-to-simple-graphql-schema
Advanced tools
Comparing version
@@ -20,7 +20,7 @@ #! /usr/bin/env node | ||
const getStdin = require("get-stdin"); | ||
const fs = require("fs"); | ||
const { jsonToSchema } = require("./lib"); | ||
const main = async () => { | ||
const jsonInput = await getStdin(); | ||
const main = () => { | ||
const jsonInput = fs.readFileSync(0, "utf8"); | ||
@@ -27,0 +27,0 @@ const { error, value } = jsonToSchema({ jsonInput }); |
@@ -20,3 +20,3 @@ /** | ||
const util = require("util"); | ||
const _ = require("lodash"); | ||
const isObject = require("lodash.isobject"); | ||
const pascalCase = require("pascal-case"); | ||
@@ -29,6 +29,6 @@ const { removeDuplicateTypes } = require("./remove-duplicate-types"); | ||
Object.keys(obj).forEach(key => { | ||
if (_.isArray(obj[key])) { | ||
if (Array.isArray(obj[key])) { | ||
const firstElement = obj[key][0]; | ||
if (_.isArray(firstElement) || _.isObject(firstElement)) { | ||
if (Array.isArray(firstElement) || isObject(firstElement)) { | ||
const newTypeName = pascalCase(key); | ||
@@ -41,3 +41,3 @@ str += schemaToString(newTypeName, firstElement); | ||
if (_.isObject(obj[key])) { | ||
if (isObject(obj[key])) { | ||
const newTypeName = pascalCase(key); | ||
@@ -60,3 +60,3 @@ str += schemaToString(newTypeName, obj[key]); | ||
module.exports = { | ||
stringifySchema: _.flow([schemaToString, removeDuplicateTypes]) | ||
stringifySchema: (typeName, obj) => removeDuplicateTypes(schemaToString(typeName, obj)) | ||
}; |
@@ -19,12 +19,16 @@ /** | ||
const _ = require("lodash"); | ||
const isInteger = require("lodash.isinteger"); | ||
const isBoolean = require("lodash.isboolean"); | ||
const isNumber = require("lodash.isnumber"); | ||
const isObject = require("lodash.isobject"); | ||
const _set = require("lodash.set"); | ||
const transformPrimitive = (key, value) => { | ||
if (_.isInteger(value)) { | ||
if (isInteger(value)) { | ||
return "Int"; | ||
} | ||
if (_.isBoolean(value)) { | ||
if (isBoolean(value)) { | ||
return "Boolean"; | ||
} | ||
if (_.isNumber(value)) { | ||
if (isNumber(value)) { | ||
return "Float"; | ||
@@ -46,6 +50,6 @@ } | ||
if (!_.isArray(currentValue) && !_.isObject(currentValue)) { | ||
if (!Array.isArray(currentValue) && !isObject(currentValue)) { | ||
const newObjValue = transformPrimitive(key, currentValue); | ||
const newObjValuePath = path ? `${path}.` : ""; | ||
_.set(result, `${newObjValuePath}${key}`, newObjValue); | ||
_set(result, `${newObjValuePath}${key}`, newObjValue); | ||
return; | ||
@@ -55,3 +59,3 @@ } | ||
// all this to guard against circular refs | ||
if (_.some(processedItemsCache, o => o === currentValue)) { | ||
if (processedItemsCache.some(o => o === currentValue)) { | ||
return; | ||
@@ -67,3 +71,3 @@ } | ||
// array elements | ||
if (_.isArray(currentValue)) { | ||
if (Array.isArray(currentValue)) { | ||
currentValue = [currentValue[0]]; | ||
@@ -70,0 +74,0 @@ } |
{ | ||
"name": "@walmartlabs/json-to-simple-graphql-schema", | ||
"description": "Converts a JSON object into a GraphQL schema", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"homepage": "https://github.com/walmartlabs/json-to-simple-graphql-schema", | ||
@@ -30,4 +30,7 @@ "bugs": { | ||
"dependencies": { | ||
"get-stdin": "6.0.0", | ||
"lodash": "4.17.11", | ||
"lodash.isboolean": "3.0.3", | ||
"lodash.isinteger": "4.0.4", | ||
"lodash.isnumber": "3.0.3", | ||
"lodash.isobject": "3.0.2", | ||
"lodash.set": "4.3.2", | ||
"pascal-case": "2.0.1" | ||
@@ -37,9 +40,9 @@ }, | ||
"babel-eslint": "10.0.1", | ||
"eslint": "5.10.0", | ||
"eslint": "5.16.0", | ||
"eslint-config-walmart": "2.2.1", | ||
"eslint-plugin-filenames": "1.3.2", | ||
"jest": "23.6.0", | ||
"nyc": "13.1.0", | ||
"prettier": "1.15.3" | ||
"jest": "24.7.0", | ||
"nyc": "13.3.0", | ||
"prettier": "1.16.4" | ||
} | ||
} |
@@ -20,3 +20,3 @@ ## json-to-simple-graphql-schema | ||
```bash | ||
curl https://demo.ckan.org/api/3/action/package_show?id=adur_district_spending \ | ||
curl https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.json?accessType=DOWNLOAD \ | ||
| npx json-to-simple-graphql-schema | ||
@@ -29,3 +29,3 @@ ``` | ||
```bash | ||
curl https://demo.ckan.org/api/3/action/package_show?id=adur_district_spending > input.json | ||
curl https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.json?accessType=DOWNLOAD > input.json | ||
@@ -35,2 +35,10 @@ cat input.json | npx json-to-simple-graphql-schema > output.graphql | ||
### Usage in the browser: | ||
```javascript | ||
import { jsonToSchema } from "@walmartlabs/json-to-simple-graphql-schema/lib"; | ||
const schema = jsonToSchema({ jsonInput: '{"name": "Test"}' }); | ||
console.log(schema.value); | ||
``` | ||
### Example output: | ||
@@ -37,0 +45,0 @@ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
14281
4.05%269
1.51%150
5.63%6
100%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed