码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-08-18 11:31:02      阅读:1672      评论:0      收藏:0      [点我收藏+]

标签:.com   --   row   data-   运行   serve   depend   save   初始化   

类似的工具比较多,比如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

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

标签:.com   --   row   data-   运行   serve   depend   save   初始化   

原文地址:https://www.cnblogs.com/rongfengliang/p/9496287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!