GithubHelp home page GithubHelp logo

mongoose2gql's Introduction

Mongoose Schema To GraphQL Object

警告

這個套件目前還在實驗階段

前言

這個套件參考了 https://github.com/sarkistlt/mongoose-schema-to-graphql

並且新增了一些目前我需要的東西,分別是:

  1. 將GraphQL Fields 定義改為閉包(Closure)的方式,讓各個Type間可以循環參照 (當然你不一定要讓他循環參照)

  2. 不採用亂數命名的方式 (命名管理起來太亂了)

  3. Array型態不單單只轉變成[String]

  4. 加入了GraphQLNonNull的支持

Install

$ npm i -S mongoose2gql

Method

/**
 * @params {string} name
 * @params {mongooseSchema} schema
 * @params {string} class Enum["GraphQLObjectType", "GraphQLInputObjectType"]
 * @params {string} description
 * @params {function} extends
 *         @return Object
 * @return GraphQLType[class]
 */
createType({
  name,
  schema,
  class,
  description,
  extends
})

Exmaple

schema

import mongoose from "mongoose"
import {
  GraphQLFloat
} from "graphql"
import b from "./b";

let Schema = mongoose.Schema;
export default Schema({
  stringType: String,
  numberType: Number,
  floatType: {
    type: Number,
    graphql: GraphQLFloat   //強制使用這個型別
  },
  dateType: Date,
  requiredType: {
    required: true,
    type: String
  },
  arrayTypeString: [String],
  arrayTypeNumber: [Number],
  tree: {
    title: String,
    tree: {
      title: String,
      titleArray: [String],
      titleObject: {
        subTitle: String,
        subsubTitle: String,
        createAt: Date
      }
    }
  },
  EmbeddedB: b,
  EmbeddedBArray: [b],
  __v: Number
}, {timestamp: true});
import createType from "../../src";
import schema from "../schema/a";
import BType from "./BType";

export default createType({
  name: "AType",
  schema,
  extends: () => ({
    BType: {
      type: BType
    }
  }),
  exclude: ["_id"]
})

這個範例會等於

type AType {
  stringType: String
  numberType: Int
  floatType: Float
  dateType: String
  requiredType: String!
  arrayTypeString: [String]
  arrayTypeNumber: [Int]
  EmbeddedB: AType_EmbeddedB
  EmbeddedBArray: [AType_EmbeddedBArray]
  tree: AType_tree
  BType: BType
}

mongoose2gql's People

Contributors

kudorori avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

skyler-cheng

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.