harbor rest api 转graphql api
Posted rongfengliang-荣锋亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了harbor rest api 转graphql api相关的知识,希望对你有一定的参考价值。
原理
实际上就是使用graphql 中的binding,首先基于swagger api 进行schema 生成,后边就是
使用binding 进行graphql 请求api 转换为rest api 请求,目前测试过两个开源的方案:
prisma 的graphql-openapi-binding 以及swagger-graphql 类库
步骤
- swagger 模型生成graphql schema
使用cli 工具 swagger-to-graphql
npm install -g swagger-to-graphql
harbor swagger 文件
https://raw.githubusercontent.com/goharbor/harbor/master/docs/swagger.yaml
可以使用swagger editor 转换为json格式,同时我们暂时需要先删除带有文件操作的api
swagger-to-graphql --swagger=/path/to/swaggerjson > ./swagger.graphql
- swagger-to-graphql 使用
比较简单,基于swagger 2 graphql npm 包
package.json:
{
"name": "swagger-graphql",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"babel-polyfill": "^6.26.0",
"express": "^4.16.3",
"express-graphql": "^0.6.12",
"graphql": "^0.13.2",
"swagger-to-graphql": "^1.4.0"
},
"scripts": {
"start": "node app"
}
}
app.js:
require(\'babel-polyfill\');
const express = require(\'express\');
const app = express();
const graphqlHTTP = require(\'express-graphql\');
const graphQLSchema = require(\'swagger-to-graphql\');
const proxyUrl = \'https://harborserver/api\';
const pathToSwaggerSchema = `${__dirname}/api/swagger.json`;
const customHeaders = {
Authorization: \'Basic YWRkOmJhc2ljQXV0aA==\'
};
graphQLSchema(pathToSwaggerSchema, proxyUrl, customHeaders).then(schema => {
app.use(\'/graphql\', graphqlHTTP(() => {
return {
schema,
graphiql: true
};
}));
app.listen(3009, \'0.0.0.0\', () => {
console.info(\'http://localhost:3009/graphql\');
});
}).catch(e => {
console.log(e);
});
- graphql-binding-openapi
类似,只是步骤多了几步
app.js
const { OpenApi } = require(\'graphql-binding-openapi\')
const { GraphQLServer } = require(\'graphql-yoga\')
const {importSchema} = require("graphql-import")
const typeDefs = importSchema("./schema.graphql")
const resolvers = {
Query: {
get_search: async (parent, args, context, info) => {
return context.harbor.query.get_search({ status: "available" }, context, info)
}
}
}
const server = new GraphQLServer({
resolvers,
typeDefs,
context: async req => ({
...req,
harbor: await OpenApi.init(\'./harbor.json\', \'https://harborapiserver\')
})
});
server.start(() => console.log(\'Server running on http://localhost:4000\'))
package.json:
{
"name": "open-api",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"graphql-binding-openapi": "^1.0.5",
"graphql-import": "^0.6.0",
"graphql-yoga": "^1.16.0"
},
"scripts": {
"start":"node app"
}
}
几个问题
- file schema type
因为字段类型file 暂时转换不支持,但是可以手工调整
转换的时候会提示file 类型未定义,解决方法,暂时删除了关于文件的部分
实际上可以集成prisma 后者apollo 自带file type 的resolver
- 访问api 登录的问题
当前测试的开放的api,大部分api是需要进行认证的,可以还有待测试
实际上当前支持basic 认证可以使用 https://username:password@harborserver/api
或者使用 Authorization: "Basic 用户名和密码的base64加密字符串" 的请求
效果
测试
- query
query {
get_repositories_top(count:3){
name
description
pull_count
}
}
- 结果
{
"data": {
"get_repositories_top": [
{
"name": "library/kubedns-amd64",
"description": null,
"pull_count": null
},
{
"name": "coredns/coredns",
"description": null,
"pull_count": null
},
{
"name": "marketing/mk-platform-order-test",
"description": null,
"pull_count": null
}
]
}
}
- 界面
参考资料
https://github.com/graphql-binding/graphql-binding-openapi
https://github.com/yarax/swagger-to-graphql#readme
https://github.com/rongfengliang/swagger-to-graphql-docker/tree/harborgraphql
以上是关于harbor rest api 转graphql api的主要内容,如果未能解决你的问题,请参考以下文章
使用nodejs+ harbor rest api 进行容器镜像迁移
使用 REST 包装 GraphQL 从 NestJS 提供 GraphQL 和 REST API
资讯 | GraphQL vs REST API 世纪Battle