- 这是一个基于mongoose, 根据字段列表快速生成Graphql API和对应的mongodb schema的工具
- A tool based on mongoose which quickly generates Graphql API and mongodb schema using provided property list.
- No, not yet.
- 主要作用还是快速创建模块的原型, 如果直接用于生产环境的话会有性能问题
- The aim is to create prototypes, if used in production, may cause perfomance problems.
import {
IMeta,
makeGraphQLSchema,
makeGraphQLPlugin /*this makes a hapi-js plugin*/
} from "graphql-mongoose"
const user:IMeta = {
type:"object",
label:"User",
name:"user", //this is the name of the mongodb schema
fields:[
{
key:"name",
label:"Name",
type:"string"
},{
key:"age",
label:"Age",
type:"number"
},{
key:"parents",
label:"Parents",
type:"ref",
ref:"user", //this field is a reference to "user" schema
},{
key:"phones",
label:"Phones",
type:"array",
item:{
key:"PhoneNumber", //this is not used for now.
label:"PhoneNumber", //this is not used for now.
type:"string"
}
},{
key:"detail",
label:"Detail",
type:"object",
fields:[
// nested fields
]
}
]
}
const schema = makeGraphQLSchema({
metas:[
user
],
querys:{}, // custom queries. same options as custom mutations.
connection // node-mongodb-native connection, you can get it from mongoose's createConnection method
mutations:{
helloWorld:{ // a custom mutation named "helloWorld", this is added in addition to the generated mutations.
args:{
name:{
meta:{
type:"string",
name:"Name",
label:"Name"
},
defaultValue:"world"
}
},
returns:{
type:"string",
name:"string",
label:"string"
},
resolve:(args,context)=>{
return "hello "+args.name
}
}
}
})
const app = require("express")()
app.use(require("body-parser").json())
app.post("/graphql",(req,res)=>{
const {variables,query} = req.body as any
return graphql(schema,query,null,request,variables)
})
app.listen()
- Now you have a running graphql api at /graphql with these queries and mutations:
- query
query {
user (search:{name:"zhuzatang"}){
}
}
- mutations
- addUser
- updateUser
- updateManyUser
- deleteUser
- helloWorld
- If you want the graphiql frontend for debuging, you can use hapi-js and makeGraphQLPlugin from this package, see ./demo/demo.ts for detail.
- install deps
- backend
- frontend (graphiql)