首先,确保node 8以上已经安装,建议用10。 确保已经安装mongodb并后台启动。
安装amur
npm install amur -g
创建项目和生成API
# 创建一个新项目
mkdir ~/Desktop/demo
cd ~/Desktop/demo
amur app . # 这里有一个点
# 不需要上传型字段的话,这一段不要执行。你需要申请个AliOSS云存储。
# 再没有Uploader的情况下,接下来的命令,请删掉上传型字段。
# 这里创建一个Uploader
amur uploader FileUploader extends AliOSSUploader bucket=your-bucket \
region=oss-cn-shanghai accessKeySecret=yourSecret \
accessKeyId=yourId
# Note about the contact schema, it's should be copied than referenced.
amur resource User 'email:String!$' 'name:String!$' 'gender:Enum{male,female}!' \
'age:Int>=0<=100!' avatar:FileUploader orders:[Order]:user contacts:[contactSchema] \
notificationSettings:{ pushNotification:Boolean email:Boolean sms:Boolean }
amur schema Contact name:String phoneNo:String address:String
# Note here:
# we save category on product side, it's because product has several categories,
# but a category has a lot of products. We take the benefit of mongoDB.
amur resource Product 'name:String!' 'description:String!' 'price:Float>=0!' \
'quantityAvailable:Int>=0!' categories:[Category] images:[FileUploader] \
comments:[Comment]:Order.comment.product coverImage:FileUploader
# We can add a 'countable' here.
amur resource Category 'name:String!$' 'description:String!' \
products:[Product]:categories parentCategory:Category \
childCategories:[Category]:parentCategory
# Note we need to write custom code to verify if the quality is valid
# We also need to add a custom getter which calculates the price
amur resource Order 'product:Product!' 'user:User!' comment:Comment \
'quality:Int>=0' contact:contactSchema
amur resource Comment 'content:String!' order:Order:comment images:[FileUploader] \
user:User:Order.user.comment product:Product:Order.product.comment
最后,npm start即可打开浏览器,到http://localhost:4000体验GraphQL API啦