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

json-schema-from-graphql

v0.9.8

Published

Converts from GraphQL Schema Language into JSON Schema

Downloads

179

Readme

json-schema-from-graphql

FORKED from https://github.com/jakubfiala/graphql-json-schema

Converts GraphQL DSL into JSON Schema for validation purposes

Difference against original version

  • [x] cleanup required field in a property
  • [x] correction on structuring type definition
  • [x] implement @validate directive on FIELD_DEFINITION for integer, number, string and array
  • [x] validation on Object (scalar JSON)
  • [x] validation on ISO DateTime (scalar DateTime)

Supported validation keywords

Arguments within @validate directive follow JSON Schema validation keywords

{
  integer: ["minimum", "maximum"],
  number: ["minimum", "maximum"],
  string: ["maxLength", "minLength", "pattern", "format"],
  array: ["maxItems", "minItems", "uniqueItems","format"]
}

Validate format on array require Array of strings.

Installation

npm install json-schema-from-graphql

Usage

const { transform, extendAjv } = require("json-schema-from-graphql");

const schema = transform(`
scalar Foo

union MyUnion = Foo | String | Float

enum MyEnum {
  FIRST_ITEM
  SECOND_ITEM
  THIRD_ITEM
}

# asdadas
# adasda
type query {
# asdadas
  field_demo: String!
  field_demo_array: [String!] @validate(format:"uuid")
  field_demo_array2: [String]!
# adasda
}
#
#
type body {

}
#
#
type body2 {

#
#
}

type WithDirective {
  field_datetime: DateTime
  field_json: JSON
  field_one: Int @validate(minimum: 10)
  field_two: Int! @validate(minimum: 10, maximum: 50)
  field_three: String! @validate(maxLength: 8, format: "date-time")
  field_four: String @validate(minLength: 5, pattern: "[abc]+")
  field_five: [String] @validate(uniqueItems: true, minItems: 3)
}

type Stuff {
  my_field: Int
  req_field: String!
  recursion: MoreStuff
  custom_scalar: Foo
  enum: MyEnum
}

type MoreStuff {
  first: [Float]
  identifier: [ID]!
  reference: Stuff!
  bool: Boolean!
  union: MyUnion
  with_params: Int
}

input InputType {
  an_int: Int!
  a_string: String
}


  `);

console.log(schema);

the code above prints the following JSON as a plain JS object:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "Foo": {
      "title": "Foo",
      "type": "GRAPHQL_SCALAR"
    },
    "MyUnion": {
      "title": "MyUnion",
      "type": "GRAPHQL_UNION",
      "oneOf": [
        {
          "$ref": "#/definitions/Foo"
        },
        {
          "type": "string"
        },
        {
          "type": "number"
        }
      ]
    },
    "MyEnum": {
      "title": "MyEnum",
      "type": "GRAPHQL_ENUM",
      "enum": ["FIRST_ITEM", "SECOND_ITEM", "THIRD_ITEM"]
    },
    "query": {
      "title": "query",
      "type": "object",
      "properties": {
        "field_demo": {
          "type": "string",
          "title": "field_demo"
        },
        "field_demo_array": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "title": "field_demo_array",
          "ext_format": "uuid"
        },
        "field_demo_array2": {
          "type": "array",
          "items": {
            "type": ["string", "null"]
          },
          "title": "field_demo_array2"
        }
      },
      "required": ["field_demo", "field_demo_array2"]
    },
    "WithDirective": {
      "title": "WithDirective",
      "type": "object",
      "properties": {
        "field_datetime": {
          "type": "string",
          "format": "date-time",
          "title": "field_datetime"
        },
        "field_json": {
          "type": "object",
          "title": "field_json"
        },
        "field_one": {
          "type": "integer",
          "title": "field_one",
          "minimum": 10
        },
        "field_two": {
          "type": "integer",
          "title": "field_two",
          "minimum": 10,
          "maximum": 50
        },
        "field_three": {
          "type": "string",
          "title": "field_three",
          "maxLength": 8,
          "format": "date-time"
        },
        "field_four": {
          "type": "string",
          "title": "field_four",
          "minLength": 5,
          "pattern": "[abc]+"
        },
        "field_five": {
          "type": "array",
          "title": "field_five",
          "minItems": 3,
          "uniqueItems": true,
          "items": {
            "type": ["string", "null"]
          }
        }
      },
      "required": ["field_two", "field_three"]
    },
    "Stuff": {
      "title": "Stuff",
      "type": "object",
      "properties": {
        "my_field": {
          "type": "integer",
          "title": "my_field"
        },
        "req_field": {
          "type": "string",
          "title": "req_field"
        },
        "recursion": {
          "$ref": "#/definitions/MoreStuff",
          "title": "recursion"
        },
        "custom_scalar": {
          "$ref": "#/definitions/Foo",
          "title": "custom_scalar"
        },
        "enum": {
          "$ref": "#/definitions/MyEnum",
          "title": "enum"
        }
      },
      "required": ["req_field"]
    },
    "MoreStuff": {
      "title": "MoreStuff",
      "type": "object",
      "properties": {
        "first": {
          "type": "array",
          "items": {
            "type": ["number", "null"]
          },
          "title": "first"
        },
        "identifier": {
          "type": "array",
          "items": {
            "type": ["string", "null"]
          },
          "title": "identifier"
        },
        "reference": {
          "$ref": "#/definitions/Stuff",
          "title": "reference"
        },
        "bool": {
          "type": "boolean",
          "title": "bool"
        },
        "union": {
          "$ref": "#/definitions/MyUnion",
          "title": "union"
        },
        "with_params": {
          "type": "integer",
          "title": "with_params"
        }
      },
      "required": ["identifier", "reference", "bool"]
    },
    "InputType": {
      "title": "InputType",
      "type": "object",
      "input": true,
      "properties": {
        "an_int": {
          "type": "integer",
          "title": "an_int"
        },
        "a_string": {
          "type": "string",
          "title": "a_string"
        }
      },
      "required": ["an_int"]
    }
  }
}