這個套件目前還在實驗階段
這個套件參考了 https://github.com/sarkistlt/mongoose-schema-to-graphql
並且新增了一些目前我需要的東西,分別是:
-
將GraphQL Fields 定義改為閉包(Closure)的方式,讓各個Type間可以循環參照 (
當然你不一定要讓他循環參照
) -
不採用亂數命名的方式 (命名管理起來太亂了)
-
Array型態不單單只轉變成
[String]
-
加入了GraphQLNonNull的支持
$ npm i -S mongoose2gql
/**
* @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
})
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
}