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

mongoose-schema-to-graphql

v2.8.1

Published

Auto types generator for graphQL schema, based on your existed Mongoose schema. Check GitHub for description.

Downloads

106

Readme

License NPM Build Status

Use your existed Mongoose schema to generate graphQL types.

Date fields supported, they get serialized to ISO date string

Full support of all graphQL "Definitions" and "Scalars" besides "GraphQLFloat", because in Mongoose schema you can use only int. numbers. But you can use extend property to pass it, details below.

This package will help you avoid typing schemas for same essence. If you already have Mongoose schema that's enough to generate graphQL type.

How it works.

First:

npm i -S mongoose-schema-to-graphql

or

yarn add mongoose-schema-to-graphql

Make sure that your graphql package is the same version as used in mongoose-schema-to-graphql or vice versa.

Then:

import createType from 'mongoose-schema-to-graphql';

createType function accept obj as argument with following structure:

const config = {
              name: 'couponType', //graphQL type's name
              description: 'Coupon base schema', //graphQL type's description
              class: 'GraphQLObjectType', //"definitions" class name
              schema: couponSchema, //your Mongoose schema "let couponSchema = mongoose.Schema({...})"
              exclude: ['_id'], //fields which you want to exclude from mongoose schema
              extend: {
                price: {type: GraphQLFloat}
              } //add custom properties or overwrite existed
          }

After you declared config. object you're ready to go. Examples below:

dbSchemas.js

export const couponSchema = mongoose.Schema({
    couponCode: Array,
    description: String,
    discountType: String,
    discountAmount: String,
    minimumAmount: String,
    singleUseOnly: Boolean,
    createdAt: mongoose.Schema.Types.Date,
    updatedAt: mongoose.Schema.Types.Date,
    expirationDate: mongoose.Schema.Types.Date,
    isMassPromo: Boolean,
    couponBatchId: String,
    maximumAmount: String,
    isPublished: Boolean
});
import createType from 'mongoose-schema-to-graphql';
import { couponSchema } from './dbSchemas';

const config = {
    name: 'couponType',
    description: 'Coupon schema',
    class: 'GraphQLObjectType',
    schema: couponSchema,
    exclude: ['_id']
};
          
export default createType(config);

It will be equal to:

import {...} from 'graphql';
import { couponSchema } from './dbSchemas';

export default new GraphQLObjectType({
    name: 'couponType',
    description: 'Coupon schema',
    fields: {
        couponCode: {type: new GraphQLList(GraphQLString)},
        description: {type: GraphQLString},
        discountType: {type: GraphQLString},
        discountAmount: {type: GraphQLString},
        minimumAmount: {type: GraphQLString},
        singleUseOnly: {type: GraphQLBoolean},
        createdAt: {type: GraphQLString},
        updatedAt: {type: GraphQLString},
        expirationDate: {type: GraphQLString},
        isMassPromo: {type: GraphQLBoolean},
        couponBatchId: {type: GraphQLString},
        maximumAmount: {type: GraphQLString},
        isPublished: {type: GraphQLBoolean}
    }
});

Note: If you pass mongoose type Array it will be converted to {type: new GraphQLList(GraphQLString)} If you want to create a list of another type, you would need to declare it in Mongoose schema too:

const quizSchema = mongoose.Schema({
    message: String,
    createdAt: mongoose.Schema.Types.Date,
    updatedAt: mongoose.Schema.Types.Date
});

export const customerSchema = mongoose.Schema({
    createdAt: mongoose.Schema.Types.Date,
    updatedAt: mongoose.Schema.Types.Date,
    firstName: String,
    lastName: String,
    email: String,
    quiz: [quizSchema],
    subscription: {
        status: String,
        plan: String,
        products: Array
    }
});

Then:

import createType from 'mongoose-schema-to-graphql';
import { customerSchema } from './dbSchemas';

const config = {
    name: 'customerType',
    description: 'Customer schema',
    class: 'GraphQLObjectType',
    schema: customerSchema,
    exclude: ['_id']
};
          
export default createType(config);

It's equal to:

import {...} from 'graphql';
import { customerSchema } from './dbSchemas';

const quizType = new GraphQLObjectType({
    name: 'quizType',
    description: 'quiz type for customer',
    fields: {
        message: {type: GraphQLString},
        updatedAt: {type: GraphQLString},
        createdAt: {type: GraphQLString}
    }
});

export default new GraphQLObjectType({
    name: 'customerType',
    description: 'Customer schema',
    fields: {
        createdAt: {type: GraphQLString},
        updatedAt: {type: GraphQLString},
        firstName: {type: GraphQLString},
        lastName: {type: GraphQLString},
        email: {type: GraphQLString},
        quiz: {type: new GraphQLList(quizType)},
        subscription: {
            type: new GraphQLObjectType({
                name: 'subscription',
                fields: () => ({
                    status: {type: GraphQLString},
                    plan: {type: GraphQLString},
                    products: {type: new GraphQLList(GraphQLString)}
                })
            })
        }
    }
});

###extend property in config object. You can use this field to pass some additional extend. to graphQL type, for example:

import { GraphQLFloat } from 'graphql';
import createType from 'mongoose-schema-to-graphql';
import { customerSchema } from './dbSchemas';

const config = {
    name: 'customerType',
    description: 'Customer schema',
    class: 'GraphQLObjectType',
    schema: customerSchema,
    exclude: ['_id'],
    extend: {
      price: {type: GraphQLFloat}
    }
};
          
export default createType(config);

It's equal to:

import {...} from 'graphql';
import { customerSchema } from './dbSchemas';

const quizType = new GraphQLObjectType({
    name: 'quizType',
    description: 'quiz type for customer',
    fields: {
        message: {type: GraphQLString},
        updatedAt: {type: GraphQLString},
        createdAt: {type: GraphQLString}
    }
});

export default new GraphQLObjectType({
    name: 'customerType',
    description: 'Customer schema',
    fields: {
        price: {type: GraphQLFloat},
        createdAt: {type: GraphQLString},
        updatedAt: {type: GraphQLString},
        firstName: {type: GraphQLString},
        lastName: {type: GraphQLString},
        email: {type: GraphQLString},
        quiz: {type: new GraphQLList(quizType)},
        subscription: {
            type: new GraphQLObjectType({
                name: 'subscription',
                fields: () => ({
                    status: {type: GraphQLString},
                    plan: {type: GraphQLString},
                    products: {type: new GraphQLList(GraphQLString)}
                })
            })
        }
    }
});

If passed extend. already exist in Mongoose schema, for example price: Number it will be overwrite with prop. we passed in config. object.

If you have any suggestion please leave me a message.

star to be up to date.