OASGraph 转换rest api graphql 试用

时间:2023-02-03 08:50:13

创建rest api

lb4 appdemo

参考提示即可

安装 OASGraph

git clone https://github.com/strongloop/oasgraph.git
cd  oasgraph
npm link
或者  npm install -g oasgraph
如果提示权限问题,使用sudo 或者加上 --unsafe-perm
这部分有点慢

启动rest api

cd appdemo 
yarn && yarn start 
默认openapi 地址是:http://127.0.0.1:3000/openapi.json

配置服务

oasgraph http://127.0.0.1:3000/openapi.json

graphql api 访问

http://127.0.0.1:3001/graphql

OASGraph 转换rest api graphql 试用
OASGraph 转换rest api graphql 试用

说明

因为OASGraph 是基于express 以及graphql、openapi 解析运行的,建议的方式是clone 代码,或者使用api 调用
模块调用格式

const express = require('express')
const graphqlHTTP = require('express-graphql')
const OASGraph = require('oasgraph') // use real name here
const app = express()

OASGraph.createGraphQlSchema(oas)
  .then(({schema}) => {
    app.use('/graphql', graphqlHTTP({
      schema,
      graphiql: true
    }))
    app.listen(3001)
  })
  .catch(err => {
    // handle errors when creating the schema
  })

参考资料

https://www.npmjs.com/package/oasgraph
https://github.com/strongloop/oasgraph
http://v4.loopback.io/getting-started-oasgraph.html