GithubHelp home page GithubHelp logo

walmartlabs / json-to-simple-graphql-schema Goto Github PK

View Code? Open in Web Editor NEW
279.0 279.0 40.0 1.71 MB

Transforms JSON input into a GraphQL schema

Home Page: https://walmartlabs.github.io/json-to-simple-graphql-schema/

License: Other

JavaScript 92.83% CSS 1.93% HTML 5.23%
graphql javascript json

json-to-simple-graphql-schema's Introduction

json-to-simple-graphql-schema

Transforms JSON input into a GraphQL schema. Try it here

Why would I use this?

Let's say you want to use an existing REST API in a GraphQL service and expose the data that API provides. You'll need to expose that data in a GraphQL schema. Without this tool, you'll have to slog through the JSON response and manually extract and convert the relevant types to GraphQL schema types. This tool attempts to provide an automated reasonable first-pass at that effort.

Installation:

For use as a command-line app use npx :) If you'd really like to install, you can do:

npm i -g @walmartlabs/json-to-simple-graphql-schema

For use in a project:

npm i @walmartlabs/json-to-simple-graphql-schema

Command Line Usage:

Pipe in some JSON. Here's a cURL JSON response piped in to this app:

curl "https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.json?accessType=DOWNLOAD" \
| npx @walmartlabs/json-to-simple-graphql-schema

You'll still need to rename the resulting main Type in the schema, unless you like AutogeneratedMainType :)

Or pipe in some JSON from a JSON file.

curl https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.json?accessType=DOWNLOAD > input.json

cat input.json | npx json-to-simple-graphql-schema > output.graphql

Optional parameters:

  • baseType = "AutogeneratedMainType"
  • prefix = ""
curl https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.json?accessType=DOWNLOAD > input.json

cat input.json | npx json-to-simple-graphql-schema --baseType BaseType --prefix Prefix > output.graphql

Usage in front-end JS:

import { jsonToSchema } from "@walmartlabs/json-to-simple-graphql-schema/lib";

const schema = jsonToSchema({ jsonInput: '{"name": "Test"}' });
console.log(schema.value);

If you need more guidance, have a look at the source for our simple web-ui.

Example output:

Given this JSON:

{
  "id": "some-id-0",
  "name": "A fun object",
  "subType": {
    "id": "some-id-1",
    "name": "A fun sub-type"
  }
}

This app will send this to stdout:

type SubType {
  id: String
  name: String,
}
type AutogeneratedMainType {
  id: String
  name: String
  subType: SubType
}

Fun features: duplicate removal

Consider this JSON with 2 color types:

{
  "id": "some-id-0",
  "name": "A fun object",
  "color": {
    "id": "color-id-1",
    "name": "Test color"
  },
  "subType": {
    "id": "some-id-1",
    "name": "A fun sub-type",
    "color": {
      "id": "color-id-1",
      "name": "Test color",
      "hex": "#ff0000"
    }
  }
}

When piped to this app, the following schema is produced:

type Color {
  id: String
  name: String
  hex: String,
}
type SubType {
  id: String
  name: String
  color: Color,
}
type AutogeneratedMainType {
  id: String
  name: String
  subType: SubType
  color: Color
}

It kept the Color type with more fields.

Fun features: calls out possible duplicates

Consider this JSON with two types containing identical fields:

{
  "id": "some-id-0",
  "name": "A fun object",
  "color": {
    "id": "color-id-1",
    "name": "Test color"
  },
  "favoriteColor": {
    "id": "color-id-1",
    "name": "Test color"
  }
}

When piped to this app, the following schema is produced:

type FavoriteColor {
  id: String
  name: String
}

type Color {
  id: String
  name: String
}

type AutogeneratedMainType {
  id: String
  name: String
  favoriteColor: FavoriteColor
  color: Color
}

# Types with identical fields:
# FavoriteColor Color

It called out the two types with identical fields.

json-to-simple-graphql-schema's People

Contributors

andresloal avatar dependabot[bot] avatar matteoredaelli avatar maxnachlinger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-to-simple-graphql-schema's Issues

Adding an option "prefix" for Type object names

I'd like to append/merge differerent schemas generated by this tool. It would be useful to be able to create all new Type objects with a prefix string in order to not create conflicts with other schemas...

Is it possible to implement it?

Thanks in advance
Matteo

ability to assign custom data types for named fields

For instance, I'm using this tool to create a graphQL schema for use with AWS Amplify, and I would like to be able to specify all fields named createdAt with a custom data type AWSDateTime. Something like --type createdAt AWSDateTime would be useful.

some character that are not supported by graphql to be shown as any object

When we have the following data, it was not represented correctly.
For example,

    "exclude": {
      "**/.hg": true,
      "**/CVS": true,
      "**/.git": true,
      "**/.svn": true,
      "**/.DS_Store": true
    },

gets converted to

Current output

type  { hg: Boolean
  git: Boolean
  svn: Boolean
  DS_Store: Boolean }

type Exclude { **/:  **/CVS: Boolean }

Expected to be AnyObject or JSONObject

Invalid typeDefs created if json key contains string `type` at end of string

Firstly, nice little tool - thanks!

I think there's a small bug in the key name matching/splitting, where if a key in the source json has the string type at the end of it, then it's split like it's a new type definition in the output.

Example: (This causes an error in types, but doesn't throw an error in the tool)

const test = {
  "atype": {
    "here": "Now"
  }
}

results incorrectly in

type A

type {
  here: String
}

type RootTypeName {
  atype: Atype
}

If the source json had camelcased or similar, it seems to work ok.

Example: (This works ok)

const test = {
  "aType": {
    "here": "Now"
  }
}

Outputs correctly:

type AType {
  here: String
}

type Main {
  aType: AType
}

Change main type name of result

Is there a way or are you just considering telling the function how to name the main type instead of AutogeneratedMainType?

Something like:

const json = { jsonInput: '{"name": "Test"}' }
const schema = jsonToSchema("useThisMainTypeName", json);

Arrays does not produce valid Schema

Given this following JSON,

{
  "name": "Example.com",
  "data": [
    {
      "Property 1": "Example.com",
      "Property 3": "Something else"
    },
    {
      "Property 2": "Something else"
    },
    {
      "Property 2": "Something else",
      "Property 3": "Something else"
    }
  ]
}

This is the output,

type Data { Property1: String Property3: String }

type AutogeneratedMainType { name: String data: [Data ] }

Property2 is omitted and there is a Property3 instead.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.