{"id":15022497,"url":"https://github.com/sarkistlt/mongoose-schema-to-graphql","last_synced_at":"2025-10-13T11:39:55.215Z","repository":{"id":57302247,"uuid":"68950247","full_name":"sarkistlt/mongoose-schema-to-graphql","owner":"sarkistlt","description":"Use Mongoose schema to generate graphQL type.","archived":false,"fork":false,"pushed_at":"2019-10-22T20:10:44.000Z","size":111,"stargazers_count":230,"open_issues_count":5,"forks_count":28,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-30T11:11:11.289Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sarkistlt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-22T18:29:34.000Z","updated_at":"2023-08-06T17:05:44.000Z","dependencies_parsed_at":"2022-09-09T15:01:03.321Z","dependency_job_id":null,"html_url":"https://github.com/sarkistlt/mongoose-schema-to-graphql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarkistlt%2Fmongoose-schema-to-graphql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarkistlt%2Fmongoose-schema-to-graphql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarkistlt%2Fmongoose-schema-to-graphql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarkistlt%2Fmongoose-schema-to-graphql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sarkistlt","download_url":"https://codeload.github.com/sarkistlt/mongoose-schema-to-graphql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247485287,"owners_count":20946398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T19:58:02.041Z","updated_at":"2025-10-13T11:39:50.187Z","avatar_url":"https://github.com/sarkistlt.png","language":"JavaScript","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"readme":"[![License](https://img.shields.io/npm/l/mongoose-schema-to-graphql.svg)](https://www.npmjs.com/package/mongoose-schema-to-graphql)\n[![NPM](https://img.shields.io/npm/v/mongoose-schema-to-graphql.svg)](https://www.npmjs.com/package/mongoose-schema-to-graphql)\n[![Build Status](https://travis-ci.org/sarkistlt/mongoose-schema-to-graphql.svg?branch=master)](https://travis-ci.org/sarkistlt/mongoose-schema-to-graphql)\n\n## Use your existing Mongoose schema to generate graphQL type-definitions.\n#### Date fields supported, they get serialized to ISO date string\n#### 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. \n\nThis package will help you  avoid typing schemas for same essence.\nIf you already have Mongoose schema that's enough to generate graphQL type.\n\n### How it works.\nFirst:\n~~~shell\nnpm i mongoose-schema-to-graphql\n~~~\n\nor\n\n~~~shell\nyarn add mongoose-schema-to-graphql\n~~~\n\nMake sure that your ```graphql``` package is the same version as used in ```mongoose-schema-to-graphql``` or vice versa.\n\nThen:\n~~~js\nimport createType from 'mongoose-schema-to-graphql';\n~~~\n\n`createType` function accept obj as argument with following structure:\n~~~js\nconst config = {\n              name: 'couponType', //graphQL type's name\n              description: 'Coupon base schema', //graphQL type's description\n              class: 'GraphQLObjectType', //\"definitions\" class name\n              schema: couponSchema, //your Mongoose schema \"let couponSchema = mongoose.Schema({...})\"\n              exclude: ['_id'], //fields which you want to exclude from mongoose schema\n              extend: {\n                price: {type: GraphQLFloat}\n              } //add custom properties or overwrite existed\n          }\n~~~\n\nAfter you declared config. object you're ready to go. Examples below:\n~~~js\ndbSchemas.js\n\nexport const couponSchema = mongoose.Schema({\n    couponCode: Array,\n    description: String,\n    discountType: String,\n    discountAmount: String,\n    minimumAmount: String,\n    singleUseOnly: Boolean,\n    createdAt: mongoose.Schema.Types.Date,\n    updatedAt: mongoose.Schema.Types.Date,\n    expirationDate: mongoose.Schema.Types.Date,\n    isMassPromo: Boolean,\n    couponBatchId: String,\n    maximumAmount: String,\n    isPublished: Boolean\n});\n~~~\n\n~~~js\nimport createType from 'mongoose-schema-to-graphql';\nimport { couponSchema } from './dbSchemas';\n\nconst config = {\n    name: 'couponType',\n    description: 'Coupon schema',\n    class: 'GraphQLObjectType',\n    schema: couponSchema,\n    exclude: ['_id']\n};\n          \nexport default createType(config);\n~~~\n \nIt will be equal to:\n~~~js\nimport {...} from 'graphql';\nimport { couponSchema } from './dbSchemas';\n\nexport default new GraphQLObjectType({\n    name: 'couponType',\n    description: 'Coupon schema',\n    fields: {\n        couponCode: {type: new GraphQLList(GraphQLString)},\n        description: {type: GraphQLString},\n        discountType: {type: GraphQLString},\n        discountAmount: {type: GraphQLString},\n        minimumAmount: {type: GraphQLString},\n        singleUseOnly: {type: GraphQLBoolean},\n        createdAt: {type: GraphQLString},\n        updatedAt: {type: GraphQLString},\n        expirationDate: {type: GraphQLString},\n        isMassPromo: {type: GraphQLBoolean},\n        couponBatchId: {type: GraphQLString},\n        maximumAmount: {type: GraphQLString},\n        isPublished: {type: GraphQLBoolean}\n    }\n});\n~~~\n\nNote: If you pass mongoose type ```Array``` it will be converted to ```{type: new GraphQLList(GraphQLString)}```\nIf you want to create a list of another type, you would need to declare it in Mongoose schema too:\n~~~js\nconst quizSchema = mongoose.Schema({\n    message: String,\n    createdAt: mongoose.Schema.Types.Date,\n    updatedAt: mongoose.Schema.Types.Date\n});\n\nexport const customerSchema = mongoose.Schema({\n    createdAt: mongoose.Schema.Types.Date,\n    updatedAt: mongoose.Schema.Types.Date,\n    firstName: String,\n    lastName: String,\n    email: String,\n    quiz: [quizSchema],\n    subscription: {\n        status: String,\n        plan: String,\n        products: Array\n    }\n});\n~~~\n\nThen: \n~~~js\nimport createType from 'mongoose-schema-to-graphql';\nimport { customerSchema } from './dbSchemas';\n\nconst config = {\n    name: 'customerType',\n    description: 'Customer schema',\n    class: 'GraphQLObjectType',\n    schema: customerSchema,\n    exclude: ['_id']\n};\n          \nexport default createType(config);\n~~~\n\nIt's equal to:\n~~~js\nimport {...} from 'graphql';\nimport { customerSchema } from './dbSchemas';\n\nconst quizType = new GraphQLObjectType({\n    name: 'quizType',\n    description: 'quiz type for customer',\n    fields: {\n        message: {type: GraphQLString},\n        updatedAt: {type: GraphQLString},\n        createdAt: {type: GraphQLString}\n    }\n});\n\nexport default new GraphQLObjectType({\n    name: 'customerType',\n    description: 'Customer schema',\n    fields: {\n        createdAt: {type: GraphQLString},\n        updatedAt: {type: GraphQLString},\n        firstName: {type: GraphQLString},\n        lastName: {type: GraphQLString},\n        email: {type: GraphQLString},\n        quiz: {type: new GraphQLList(quizType)},\n        subscription: {\n            type: new GraphQLObjectType({\n                name: 'subscription',\n                fields: () =\u003e ({\n                    status: {type: GraphQLString},\n                    plan: {type: GraphQLString},\n                    products: {type: new GraphQLList(GraphQLString)}\n                })\n            })\n        }\n    }\n});\n~~~\n\n###```extend``` property in config object.\nYou can use this field to pass some additional extend. to graphQL type, for example:\n~~~js\nimport { GraphQLFloat } from 'graphql';\nimport createType from 'mongoose-schema-to-graphql';\nimport { customerSchema } from './dbSchemas';\n\nconst config = {\n    name: 'customerType',\n    description: 'Customer schema',\n    class: 'GraphQLObjectType',\n    schema: customerSchema,\n    exclude: ['_id'],\n    extend: {\n      price: {type: GraphQLFloat}\n    }\n};\n          \nexport default createType(config);\n~~~\n\nIt's equal to:\n~~~js\nimport {...} from 'graphql';\nimport { customerSchema } from './dbSchemas';\n\nconst quizType = new GraphQLObjectType({\n    name: 'quizType',\n    description: 'quiz type for customer',\n    fields: {\n        message: {type: GraphQLString},\n        updatedAt: {type: GraphQLString},\n        createdAt: {type: GraphQLString}\n    }\n});\n\nexport default new GraphQLObjectType({\n    name: 'customerType',\n    description: 'Customer schema',\n    fields: {\n        price: {type: GraphQLFloat},\n        createdAt: {type: GraphQLString},\n        updatedAt: {type: GraphQLString},\n        firstName: {type: GraphQLString},\n        lastName: {type: GraphQLString},\n        email: {type: GraphQLString},\n        quiz: {type: new GraphQLList(quizType)},\n        subscription: {\n            type: new GraphQLObjectType({\n                name: 'subscription',\n                fields: () =\u003e ({\n                    status: {type: GraphQLString},\n                    plan: {type: GraphQLString},\n                    products: {type: new GraphQLList(GraphQLString)}\n                })\n            })\n        }\n    }\n});\n~~~\n\nIf passed extend. already exist in Mongoose schema, for example ```price: Number``` it will be overwrite with prop. we passed in config. object. \n\nIf you have any suggestion please leave me a message.\n##### star to be up to date.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsarkistlt%2Fmongoose-schema-to-graphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsarkistlt%2Fmongoose-schema-to-graphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsarkistlt%2Fmongoose-schema-to-graphql/lists"}