@walmartlabs/json-to-simple-graphql-schema
Advanced tools
Comparing version
@@ -25,3 +25,3 @@ /** | ||
const transformPrimitive = (key, value) => { | ||
const transformPrimitive = value => { | ||
if (isInteger(value)) { | ||
@@ -39,10 +39,15 @@ return "Int"; | ||
const badTypeNameRegex = /[\W]+/g; | ||
const cleanName = name => name.replace(badTypeNameRegex, ""); | ||
const toSchema = input => { | ||
const result = {}; | ||
const processedItemsCache = []; | ||
const stack = [{ obj: input, path: "" }]; | ||
const stack = [{ obj: input, path: "", cleanedPath: "" }]; | ||
while (stack.length > 0) { | ||
const { obj, path } = stack.pop(); | ||
const { obj, path, cleanedPath } = stack.pop(); | ||
// eslint-disable-next-line max-statements | ||
Object.keys(obj).forEach(key => { | ||
@@ -52,5 +57,5 @@ let currentValue = obj[key]; | ||
if (!Array.isArray(currentValue) && !isObject(currentValue)) { | ||
const newObjValue = transformPrimitive(key, currentValue); | ||
const newObjValuePath = path ? `${path}.` : ""; | ||
_set(result, `${newObjValuePath}${key}`, newObjValue); | ||
const newObjValue = transformPrimitive(currentValue); | ||
const newObjValuePath = cleanedPath ? `${cleanedPath}.` : ""; | ||
_set(result, `${newObjValuePath}${cleanName(key)}`, newObjValue); | ||
return; | ||
@@ -69,2 +74,5 @@ } | ||
const cleanedPathPrefix = cleanedPath ? `${cleanedPath}.` : ""; | ||
const newCleanedPath = `${cleanedPathPrefix}${cleanName(key)}`; | ||
// only use the first element in the array since we're assuming the same type for all | ||
@@ -78,3 +86,4 @@ // array elements | ||
obj: currentValue, | ||
path: newPath | ||
path: newPath, | ||
cleanedPath: newCleanedPath | ||
}); | ||
@@ -81,0 +90,0 @@ }); |
{ | ||
"name": "@walmartlabs/json-to-simple-graphql-schema", | ||
"description": "Converts a JSON object into a GraphQL schema", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/walmartlabs/json-to-simple-graphql-schema", | ||
@@ -23,7 +23,8 @@ "bugs": { | ||
"scripts": { | ||
"format": "node_modules/.bin/prettier --write './**/*.js'", | ||
"pretest": "npm run format; npm run lint", | ||
"coverage": "nyc --reporter=text --reporter=html jest", | ||
"lint": "eslint .", | ||
"test": "jest" | ||
"format": "$(npm bin)/prettier --write './**/*.js'", | ||
"pretest": "npm run format && npm run lint", | ||
"lint": "$(npm bin)/eslint .", | ||
"test": "$(npm bin)/jest --coverage .", | ||
"web-ui-start": "$(npm bin)/webpack-dev-server --config ./web-ui/webpack.dev.js", | ||
"web-ui-build": "$(npm bin)/webpack --config ./web-ui/webpack.prod.js" | ||
}, | ||
@@ -39,10 +40,26 @@ "dependencies": { | ||
"devDependencies": { | ||
"@babel/core": "7.4.3", | ||
"@babel/preset-env": "7.4.3", | ||
"autoprefixer": "9.5.0", | ||
"babel-eslint": "10.0.1", | ||
"babel-loader": "8.0.5", | ||
"clean-webpack-plugin": "2.0.1", | ||
"css-loader": "2.1.1", | ||
"cssnano": "4.1.10", | ||
"eslint": "5.16.0", | ||
"eslint-config-walmart": "2.2.1", | ||
"eslint-plugin-filenames": "1.3.2", | ||
"jest": "24.7.0", | ||
"nyc": "13.3.0", | ||
"prettier": "1.16.4" | ||
"html-webpack-inline-source-plugin": "0.0.10", | ||
"html-webpack-plugin": "3.2.0", | ||
"jest": "24.7.1", | ||
"mini-css-extract-plugin": "0.5.0", | ||
"optimize-css-assets-webpack-plugin": "5.0.1", | ||
"postcss-loader": "3.0.0", | ||
"prettier": "1.16.4", | ||
"style-loader": "0.23.1", | ||
"webpack": "4.29.6", | ||
"webpack-bundle-analyzer": "3.1.0", | ||
"webpack-cli": "3.3.0", | ||
"webpack-dev-server": "3.2.1" | ||
} | ||
} |
## json-to-simple-graphql-schema | ||
Transforms streamed 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/) | ||
@@ -11,8 +12,12 @@ ### Why would I use this? | ||
No need to install this, use `npx` :) If you'd really like to install, you can do: | ||
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 | ||
npm i @walmartlabs/json-to-simple-graphql-schema | ||
``` | ||
### Usage: | ||
### Command Line Usage: | ||
@@ -34,3 +39,3 @@ Pipe in some JSON. Here's a cURL JSON response piped in to this app: | ||
### Usage in the browser: | ||
### Usage in front-end JS: | ||
```javascript | ||
@@ -42,2 +47,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). | ||
@@ -44,0 +50,0 @@ ### Example output: |
15589
9.19%275
2.23%156
4%23
228.57%