Tutorial:五分钟做一套GraphQL API,不写代码
发布于 1 年前 作者 cheunghy 2116 次浏览 来自 分享

首先,确保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啦

8 回复

这里创建一个Uploader

amur uploader FileUploader extends AliOSSUploader bucket=your-bucket
region=oss-cn-shanghai accessKeySecret=yourSecret
accessKeyId=yourId 这里换成腾讯云存储可以吗? 按照顺序执行结果:package.json: { “name”: “demo”, “version”: “0.0.1”, “private”: true, “main”: “server.js”, “scripts”: { “start”: “nodemon server.js --ignore-stderr”, “console”: “dobukulbira”, “seed”: “nonula seed”, “drop”: “nonula drop” }, “dependencies”: {}, “devDependencies”: {} } 没有依赖

HI @sonong , 项目是否父目录下面有package.json,导致npm和amur找错了项目根目录?

目前只支持阿里的oss

@cheunghy 是空的目录,如下操作步骤截图 1.png 2.png 3.png 4.png

Customize Amur Behavior Create a file called .amurrc.json in project’s root directory. And filling it like this:

{ “schemaDir”: “graphql”, “resolverDir”: “graphql”, “test”: false } Amur will generate schema files and resolver files into graphql directory, and will not generate unit tests.amur.png

目录结构如amur,所有的schemas放一起,还是如egg-graphql,一个文件夹一个model好? 目录.png

Hi @sonong please use the newest version of amur. And when creating app, use amur app any-name --schema-dir='graphql' --resolver-dir='graphql' You don’t need to modify .amurrc.json yourselves.

Hi @sonong , this is not a bug, see my screenshot. Screen Shot 2019-04-22 at 12.12.54 PM.png

回到顶部