@walmartlabs/json-to-simple-graphql-schema
Advanced tools
Comparing version
@@ -24,2 +24,3 @@ #! /usr/bin/env node | ||
const { hideBin } = require("yargs/helpers"); | ||
const argv = yargs(hideBin(process.argv)).options({ | ||
@@ -30,3 +31,3 @@ baseType: { | ||
alias: "b", | ||
default: "AutogeneratedMainType" | ||
default: "AutogeneratedMainType", | ||
}, | ||
@@ -37,4 +38,4 @@ prefix: { | ||
alias: "p", | ||
default: "" | ||
} | ||
default: "", | ||
}, | ||
}).argv; | ||
@@ -48,3 +49,3 @@ | ||
prefix, | ||
jsonInput | ||
jsonInput, | ||
}); | ||
@@ -51,0 +52,0 @@ if (error) { |
@@ -22,3 +22,3 @@ /** | ||
const safeFnExecute = fn => { | ||
const safeFnExecute = (fn) => { | ||
try { | ||
@@ -31,3 +31,3 @@ return { value: fn() }; | ||
const validateJson = jsonInput => { | ||
const validateJson = (jsonInput) => { | ||
const { error: jsonError, value } = safeFnExecute(() => | ||
@@ -52,3 +52,3 @@ JSON.parse(jsonInput) | ||
prefix = "", | ||
jsonInput | ||
jsonInput, | ||
}) => { | ||
@@ -67,3 +67,3 @@ const { error, value } = validateJson(jsonInput); | ||
jsonToSchema, | ||
validateJson | ||
validateJson, | ||
}; |
@@ -19,7 +19,7 @@ /** | ||
const combineTypes = str => | ||
const combineTypes = (str) => | ||
` ${str}` | ||
.split(" type ") | ||
// clear empty lines | ||
.filter(i => i) | ||
.filter((i) => i) | ||
.reduce((accum, fragment, idx) => { | ||
@@ -31,5 +31,5 @@ const full = `type ${fragment}`.trim(); | ||
.split(" ") | ||
.map(s => s.trim()); | ||
.map((s) => s.trim()); | ||
const name = fragmentArr.shift(); | ||
const amtFields = fragmentArr.filter(s => s.indexOf(":") > -1).length; | ||
const amtFields = fragmentArr.filter((s) => s.indexOf(":") > -1).length; | ||
@@ -44,7 +44,7 @@ // keep the type with the most fields | ||
const extractPossibleIdenticalTypes = str => | ||
const extractPossibleIdenticalTypes = (str) => | ||
str | ||
.split("type ") | ||
// clear empty lines | ||
.filter(i => i) | ||
.filter((i) => i) | ||
.reduce((accum, fragment) => { | ||
@@ -54,3 +54,3 @@ const fragmentArr = fragment | ||
.split(" ") | ||
.map(s => s.trim()); | ||
.map((s) => s.trim()); | ||
const name = fragmentArr.shift(); | ||
@@ -70,3 +70,3 @@ const fieldsSignature = fragmentArr.join(" "); | ||
const removeDuplicateTypes = str => { | ||
const removeDuplicateTypes = (str) => { | ||
const schemaStr = Object.values(combineTypes(str)) | ||
@@ -80,4 +80,4 @@ .sort((a0, a1) => a0.idx - a1.idx) | ||
) | ||
.filter(arr => arr.length > 1) | ||
.map(arr => `# ${arr.join(" ")}`) | ||
.filter((arr) => arr.length > 1) | ||
.map((arr) => `# ${arr.join(" ")}`) | ||
.join("\n"); | ||
@@ -84,0 +84,0 @@ |
@@ -27,3 +27,3 @@ /** | ||
Object.keys(obj).forEach(key => { | ||
Object.keys(obj).forEach((key) => { | ||
if (Array.isArray(obj[key])) { | ||
@@ -65,3 +65,3 @@ const firstElement = obj[key][0]; | ||
stringifySchema: (typeName, prefix, obj) => | ||
removeDuplicateTypes(schemaToString(typeName, prefix, obj)) | ||
removeDuplicateTypes(schemaToString(typeName, prefix, obj)), | ||
}; |
@@ -25,3 +25,3 @@ /** | ||
const transformPrimitive = value => { | ||
const transformPrimitive = (value) => { | ||
if (isInteger(value)) { | ||
@@ -41,5 +41,5 @@ return "Int"; | ||
const cleanName = name => name.replace(badTypeNameRegex, ""); | ||
const cleanName = (name) => name.replace(badTypeNameRegex, ""); | ||
const toSchema = input => { | ||
const toSchema = (input) => { | ||
const result = {}; | ||
@@ -53,3 +53,3 @@ const processedItemsCache = []; | ||
// eslint-disable-next-line max-statements | ||
Object.keys(obj).forEach(key => { | ||
Object.keys(obj).forEach((key) => { | ||
let currentValue = obj[key]; | ||
@@ -65,3 +65,3 @@ | ||
// all this to guard against circular refs | ||
if (processedItemsCache.some(o => o === currentValue)) { | ||
if (processedItemsCache.some((o) => o === currentValue)) { | ||
return; | ||
@@ -87,3 +87,3 @@ } | ||
path: newPath, | ||
cleanedPath: newCleanedPath | ||
cleanedPath: newCleanedPath, | ||
}); | ||
@@ -90,0 +90,0 @@ }); |
{ | ||
"name": "@walmartlabs/json-to-simple-graphql-schema", | ||
"description": "Converts a JSON object into a GraphQL schema", | ||
"version": "2.0.6", | ||
"version": "3.0.0", | ||
"homepage": "https://github.com/walmartlabs/json-to-simple-graphql-schema", | ||
@@ -14,5 +14,2 @@ "bugs": { | ||
"main": "app.js", | ||
"engines": { | ||
"node": ">=8.0.0" | ||
}, | ||
"license": "Apache-2.0", | ||
@@ -24,42 +21,27 @@ "author": "Store Services", | ||
"scripts": { | ||
"format": "prettier --write './**/*.js'", | ||
"pretest": "npm run format && npm run lint", | ||
"lint": "eslint .", | ||
"test": "jest --coverage .", | ||
"web-ui-start": "webpack-dev-server --config ./web-ui/webpack.dev.js", | ||
"web-ui-build": "webpack --config ./web-ui/webpack.prod.js" | ||
"build-web-ui": "webpack", | ||
"pretest": "npm run lint", | ||
"lint": "prettier --check . && eslint .", | ||
"lint-fix": "prettier --write . && eslint . --fix", | ||
"test": "jest --coverage ." | ||
}, | ||
"dependencies": { | ||
"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": "^3.1.1", | ||
"yargs": "^16.1.0" | ||
"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": "3.1.2", | ||
"util": "0.12.4", | ||
"yargs": "17.3.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.11.1", | ||
"@babel/preset-env": "^7.11.0", | ||
"autoprefixer": "^9.8.6", | ||
"babel-eslint": "^10.1.0", | ||
"babel-loader": "^8.1.0", | ||
"clean-webpack-plugin": "^3.0.0", | ||
"css-loader": "^4.2.1", | ||
"cssnano": "4.1.10", | ||
"eslint": "^7.6.0", | ||
"eslint-config-walmart": "^2.2.1", | ||
"eslint-plugin-filenames": "1.3.2", | ||
"html-webpack-plugin": "4.3.0", | ||
"jest": "^26.3.0", | ||
"mini-css-extract-plugin": "^0.10.0", | ||
"optimize-css-assets-webpack-plugin": "^5.0.3", | ||
"postcss-loader": "3.0.0", | ||
"prettier": "^2.0.5", | ||
"style-loader": "^1.2.1", | ||
"webpack": "^4.44.1", | ||
"webpack-bundle-analyzer": "^3.8.0", | ||
"webpack-cli": "^3.3.12", | ||
"webpack-dev-server": "^3.11.0" | ||
"css-loader": "6.5.1", | ||
"eslint": "8.5.0", | ||
"jest": "27.4.5", | ||
"prettier": "2.5.1", | ||
"style-loader": "3.3.1", | ||
"webpack": "5.65.0", | ||
"webpack-cli": "4.9.1" | ||
} | ||
} |
## json-to-simple-graphql-schema | ||
Transforms JSON input into a GraphQL schema. | ||
Transforms JSON input into a GraphQL schema. | ||
[Try it here](https://walmartlabs.github.io/json-to-simple-graphql-schema/) | ||
@@ -13,6 +13,9 @@ | ||
For use as a command-line app use `npx` :) If you'd really like to install, you can do: | ||
```bash | ||
npm i -g @walmartlabs/json-to-simple-graphql-schema | ||
``` | ||
For use in a project: | ||
```bash | ||
@@ -25,2 +28,3 @@ npm i @walmartlabs/json-to-simple-graphql-schema | ||
Pipe in some JSON. Here's a cURL JSON response piped in to this app: | ||
```bash | ||
@@ -30,2 +34,3 @@ curl "https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.json?accessType=DOWNLOAD" \ | ||
``` | ||
> You'll still need to rename the resulting main Type in the schema, unless you like `AutogeneratedMainType` :) | ||
@@ -42,2 +47,3 @@ | ||
Optional parameters: | ||
- baseType = "AutogeneratedMainType" | ||
@@ -53,2 +59,3 @@ - prefix = "" | ||
### Usage in front-end JS: | ||
```javascript | ||
@@ -60,2 +67,3 @@ import { jsonToSchema } from "@walmartlabs/json-to-simple-graphql-schema/lib"; | ||
``` | ||
If you need more guidance, have a look at [the source for our simple web-ui](./web-ui). | ||
@@ -66,2 +74,3 @@ | ||
Given this JSON: | ||
```json | ||
@@ -77,11 +86,13 @@ { | ||
``` | ||
This app will send this to stdout: | ||
``` | ||
type SubType { | ||
id: String | ||
type SubType { | ||
id: String | ||
name: String, | ||
} | ||
type AutogeneratedMainType { | ||
id: String | ||
name: String | ||
} | ||
type AutogeneratedMainType { | ||
id: String | ||
name: String | ||
subType: SubType | ||
@@ -94,2 +105,3 @@ } | ||
Consider this JSON with 2 `color` types: | ||
```json | ||
@@ -114,21 +126,24 @@ { | ||
``` | ||
When piped to this app, the following schema is produced: | ||
``` | ||
type Color { | ||
id: String | ||
name: String | ||
type Color { | ||
id: String | ||
name: String | ||
hex: String, | ||
} | ||
type SubType { | ||
id: String | ||
name: String | ||
} | ||
type SubType { | ||
id: String | ||
name: String | ||
color: Color, | ||
} | ||
type AutogeneratedMainType { | ||
id: String | ||
name: String | ||
subType: SubType | ||
} | ||
type AutogeneratedMainType { | ||
id: String | ||
name: String | ||
subType: SubType | ||
color: Color | ||
} | ||
``` | ||
It kept the `Color` type with more fields. | ||
@@ -139,2 +154,3 @@ | ||
Consider this JSON with two types containing identical fields: | ||
```json | ||
@@ -154,3 +170,5 @@ { | ||
``` | ||
When piped to this app, the following schema is produced: | ||
``` | ||
@@ -177,2 +195,3 @@ type FavoriteColor { | ||
``` | ||
It called out the two types with identical fields. |
7
-68.18%10
11.11%343
9.24%185
11.45%16564
-0.35%8
14.29%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
Updated
Updated
Updated
Updated
Updated
Updated
Updated