npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

v3.0.1

Published

Converts a JSON object into a GraphQL schema

Downloads

3,579

Readme

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.