使用graphql-code-generator 生成graphql 代码

类似的工具比较多,比如prisma 、qloo、golang 的gqlgen、apollo-codegen
graphql-code-generator 也是一个不错的工具(灵活、模版自定义。。。)

安装

 npm install --save-dev graphql-code-generator graphql
 Or
 yarn add -D graphql-code-generator graphql

参考demo

  • 项目初始化
yarn init -y 
  • 添加依赖
 yarn add --dev graphql-code-generator graphql
package.json:

{
"name": "first",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"graphql": "^0.13.2",
"graphql-code-generator": "^0.10.7",
"graphql-codegen-typescript-template": "^0.10.7",
"typescript": "^3.0.1"
},
"scripts": {
"gen": "gql-gen --schema http://localhost:3009/graphql --template graphql-codegen-typescript-template --out ./typings/ \"./src/**/*.graphql\"",
"code":"tsc "
}
}
  • clone 测试graphql server
git clone https://github.com/rongfengliang/harborapi2graphql.git
  • 启动测试graphql server
yarn && yarn start

运行生成graphql 代码

  • 生成代码
yarn  gen

参考效果

参考资料

https://github.com/rongfengliang/graphql-code-generate-demo
https://github.com/dotansimha/graphql-code-generator

posted on 2018-08-18 09:56  荣锋亮  阅读(1734)  评论(0编辑  收藏  举报

导航