mongoose-graphql

0.0.8 • Public • Published

mongoose-graphql CircleCI

mongoose-graphql converts a mongoose model to graphql types.

Installation

Using npm:

$ npm install --save mongoose-graphql
// using ES6 modules
import { modelToType } from 'mongoose-graphql';
 
// using CommonJS modules
var mongooseGraphQL = require('mongoose-graphql');
var modelToType = mongooseGraphQL.modelToType;

API

modelToType

modelToType(model, options)

Convert a mongoose model to graphql types.

You can use this type definition in graphql-tools to build an executable schema.

const CategorySchema = new Schema({
  type: String,
});
 
const BookModel = mongoose.model('Book', new Schema({
  category: CategorySchema,
  name: String,
}));
 
const typeDef = modelToType(BookModel, {
  extend: {
    Book: {
      publishers: '[Publisher]',
    },
    BookCategory: {
      genre: 'Genre',
    },
  },
});
 
console.log(typeDef);

Outputs:

type BookCategory {
  _id: String
  genre: Genre
  type: String
}
type Book {
  _id: String
  category: BookCategory
  name: String
  publishers: [Publisher]
}

/mongoose-graphql/

    Package Sidebar

    Install

    npm i mongoose-graphql

    Weekly Downloads

    4

    Version

    0.0.8

    License

    MIT

    Last publish

    Collaborators

    • jperl