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 🙏

© 2025 – Pkg Stats / Ryan Hefner

jsonschema-to-graphql

v0.0.4

Published

Convert JsonSchema to GraphQL Schema

Readme

jsonschema-to-graphql

A GraphQL schema generated by reading JSON schemas.

Generate the GraphQL Schema (Non-Relay Compliant) by providing JSON Schema files. Refer examples folder for more sample implementation.
Default knex-based resolver is provided. You can extend the resolver to add your own implementation.

Installation

npm install jsonschema-to-graphql

Running Sample Locally

npm run sample

Usage

var GraphQL = require('jsonschema-to-graphql');
// Add a Resolver, Can be replace with custom resolvers 
var Resolver = require('../dist/knex-resolver');
var resolver = new Resolver(knex);

var opts = {
  resolver : resolver,
  skipConstraintModels: false,
  skipOperatorFields: false,
  skipPaginationFields: false,
  skipSortByFields: false
};

// Build GraphQL Schema
// This is all you need to do to generate the schema.
var graphQLSchema  = GraphQL.builder(opts)
	.addSchema(require('./schemas/User'), {
    exclude: ['ignoreField']
  })
  .addSchema(require('./schemas/Movie'), {})
  .addCustomQueryFunction(require('./schemas/CustomFunction'))
  .build();

Builder accepts opts parameter:

  • resolver: default resolver is a no-op function return empty array/object.
  • skipConstraintModels: to skip Models generated based on primary keys and unique keys.
  • skipOperatorFields: to skip Operator fields in the list models.
  • skipPaginationFields: to skip pagination fields.
  • skipSortByFields: to skip order related fields.

addSchema accepts two parameters - schema definition and options.
addCustomQueryFunction can be used to add custom query functions.
addCustomMutationFunction can be used to add custom mutation functions.

Sample Schema

{
  tableName: 'Movie',
  description: 'Movie Model',
  primaryKey: 'id',
  foreignKeys: [{
    // Name of the association
    name: 'user',
    description: 'Owner of Movie',
    // joins from
    fields: 'userId',
    // joins to
    reference: {
      datapackage: '',
      resource: 'user',
      fields: 'id'
    }
  }],
  fields: [{
    name: 'id',
    description: 'Movie Id',
    type: 'integer',
    constraints: {
      required: true,
      autoincrement: true
    }
  }, {
    name: 'userId',
    description: 'Movie Owner\'s Id',
    type: 'integer'
  }, {
    name: 'title',
    description: 'Movie Title',
    type: 'string'
  }]
}

Note: Convention followed is tableName is singular and the association are plural.

primarykey: can also be an array of fields.
foreignKeys: bi-directional associations are generated based on singular/plural association name.
type: supports primitive data-types.

Schema Generated

For the above Movie example the schema would be something like -

// Basic GraphQL For Model
User(id: Int!): User

// Model for unique fields
UserByUserName(userName: String!): User

// Model for required fields.
UsersByEmail(email: String!, limit: Int, offset: Int, sortBy: UserPropertiesEnum, sortByDesc: UserPropertiesEnum): [User]

// Basic Model
Movie(id: Int!): Movie  

// List Model, with operators.
Movies(  
id_Gt: Int,   
id_Gte: Int,   
id_Lt: Int,   
id_Lte: Int,   
id_Ne: Int,   
id_In: [Int]  
id_NotIn: [Int]  
id: Int,  
id_IsNull: Boolean, ...  
limit: Int,  
offset: Int,  
sortBy: MoviePropertiesEnum,  
sortByDesc: MoviePropertiesEnum): [Movie]  
)

Each of the Model will also contain bi-directional links to fetch related data. for instance, User model will contain movies link, and Movie model will contain user link.

Mutation Support

Basic Mutations for each model is supported through Create<Model>, Update<Model> and Delete<Model>.

// For creating a Movie
CreateMovie(userId: Int, title: String): CreateMovie
// Updating a Movie
UpdateMovie(id: Int!,userId: Int, title: String): UpdateMovie
// Deleting a Movie
DeleteMovie(id: Int!): DeleteMovie