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的更多相关文章
- 一个方便查看数据库转换rest/graphql api 的开源软件的github 项目
https://github.com/dbohdan/automatic-api 是一个不错的github 知识项目,帮助我们 列出了,常见的的数据库可以直接转换为rest/graphql api 的 ...
- 通过torodb && hasura graphql 让mongodb 快速支持graphql api
torodb 可以方便的将mongo 数据实时同步到pg,hasura graphql 可以方便的将pg 数据暴露为graphql api,集成在一起真的很方便 环境准备 docker-compose ...
- 【Graphql实践】使用 Apollo(iOS) 访问 Github 的 Graphql API
最近在协助调研 Apollo 生成的代码是否有可能跨 Query 共享模型的问题,虽然初步结论是不能,并不是预期的结果,但是在调研过程中积累的一些经验,有必要记录下.如果你也对 Graphql 感兴趣 ...
- [GraphQL] Query a GraphQL API with graphql-request
To query a GraphQL API, all you need to do is send an HTTP request that includes the query operation ...
- 人人都是 API 设计师:我对 RESTful API、GraphQL、RPC API 的思考
原文地址:梁桂钊的博客 博客地址:http://blog.720ui.com 欢迎关注公众号:「服务端思维」.一群同频者,一起成长,一起精进,打破认知的局限性. 有一段时间没怎么写文章了,今天提笔写一 ...
- haproxy 2.0 dataplaneapi rest api 转为graphql docker 镜像
为了方便直接使用haproxy dataplaneapi graphql 格式的查询,制作了一个简单的docker 镜像 基于dotenv 进行配置管理,可以直接通过环境变量传入参数,处理不同hapr ...
- haproxy 2.0 dataplaneapi rest api 转为graphql
haproxy 2.0 dataplaneapi rest api 是比较全的,以下是一个简单的集成graphql,通过swagger-to-graphql 转换为graphql api 方便使用 环 ...
- GitHub GraphQL API v4 & GitHub REST API v3
GitHub, GraphQL API, v4 ,REST API, v3, GraphQL, https://developer.github.com/v4/ https://developer.g ...
- GitHub & GraphQL API
GitHub & GraphQL API https://gist.github.com/xgqfrms/15559e7545f558d85c5efdea79171a3d refs xgqfr ...
随机推荐
- 牛客国庆集训派对Day6 Solution
A Birthday 思路:设置一个源点,一个汇点,每次对$源点对a_i, b_i , a_i 对 b_i 连一条流为1,费用为0的边$ 每个点都再连一条 1, 3, 5, 7, ....的边到 ...
- 《FontForge常见问题FAQ》字王翻译版
<FontForge常见问题FAQ> 字王翻译版 原文: http://fontforge.github.io/en-US/faq/ 翻译: 字王·中国 blog: http://bl ...
- 466E - Information Graph 巧妙的判断祖先于孩子的关系
这题说的是给了一个公司员工100000 然后现在又3种操作第一种将y置为x的父亲,第二种操作将文件给第x个人签他签完给他的上司签,一直到没有上司为止,第三种操作问x是否签了第i份文件,然后 我们只要直 ...
- ng-深度学习-课程笔记-17: 序列模型和注意力机制(Week3)
1 基础模型(Basic models) 一个机器翻译的例子,比如把法语翻译成英语,如何构建一个神经网络来解决这个问题呢? 首先用RNN构建一个encoder,对法语进行编码,得到一系列特征 然后用R ...
- Refactoring #002 Inline Method
Example private ServerSocket createServerSocket(final int port) throws IOException { ServerSocket re ...
- 2017-2018-1 JaWorld 第三周作业
2017-2018-1 JaWorld 第三周作业 团队展示 队员学号 队名 团队项目描述 队员风采 团队的特色 团队合照 团队初步合作 前两周的反思与总结 需要改进的地方 团队选题 *采访老师或有开 ...
- vs+qt使用资源文件
1.在Resources目录新建一个.qrc文件 2.在解决方案的Resource Files中添加这个文件 3.为这个qrc添加资源,建议把资源都放进Resources
- godaddy之ssl申请
第一步 执行下面命令生成csr和key文件 openssl req -new -newkey rsa: -nodes -keyout trips.com.key -out trips.com.csr ...
- NOIP 2018退役祭
Day 0 实在是没啥特别想干的...路上看了一下FE的小玉的第四周目的视频...然后到了之后整理了一下东西,然后被slr教着学了一下一个叫翻棋的东西,然后立刻就上瘾了...然后就听slr先生教我滑铁 ...
- 分布式缓存--系列1 -- Hash环/一致性Hash原理
当前,Memcached.Redis这类分布式kv缓存已经非常普遍.从本篇开始,本系列将分析分布式缓存相关的原理.使用策略和最佳实践. 我们知道Memcached的分布式其实是一种“伪分布式”,也就是 ...