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是需要进行认证的,可以还有待测试

效果

测试

  • 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

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

时间: 2024-08-29 18:11:14

harbor rest api 转graphql api的相关文章

【Graphql实践】使用 Apollo(iOS) 访问 Github 的 Graphql API

最近在协助调研 Apollo 生成的代码是否有可能跨 Query 共享模型的问题,虽然初步结论是不能,并不是预期的结果,但是在调研过程中积累的一些经验,有必要记录下.如果你也对 Graphql 感兴趣,不妨先从 Github 的 Graphql API 来切手实践. 关于 Graphql 官网 尽管只是做客户端的一些实践,我还是建议你先过一遍 Graphql 官网的学习指南.这样能更快速地理解概念.一些客户端库的文档,大部分都只是介绍基本用法. 中文版,可以直接看 http://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 in the body of the request. In this lesson, we will use the browser’s fetch method to request the total days skied from our GraphQL API. const query

通过beego快速创建一个Restful风格API项目及API文档自动化(转)

通过beego快速创建一个Restful风格API项目及API文档自动化 本文演示如何快速(一分钟内,不写一行代码)的根据数据库及表创建一个Restful风格的API项目,及提供便于在线测试API的界面. 一.创建数据库及数据表(MySQL) #db--jeedev -- ---------------------------- -- Table structure for `app` -- ---------------------------- DROP TABLE IF EXISTS `a

LwIP - raw/callback API、协议栈API(sequential API)、BSD API(或者说 SOCKET API)

1.使用raw/callback API编程,用户编程的方法是向内核注册各种自定义的回调函数,回调函数是与内核实现交换的唯一方式. recv_udp, accept_function, sent_tcp, recv_tcp, do_connected, poll_tcp, err_tcp! 2.协议栈API(sequential API)是基于raw/callback API实现的,它与内核交换的方式也只能通过回调. netconn_new, netconn_delete, netconn_ge

关于CUDA两种API:Runtime API 和 Driver API

CUDA 眼下有两种不同的 API:Runtime API 和 Driver API,两种 API 各有其适用的范围. 高级API(cuda_runtime.h)是一种C++风格的接口,构建于低级API之上.因为 runtime API 较easy使用,一開始我们会以 runetime API 为主:

用API网关把API管起来

最开始只是想找个API网关防止API被恶意请求,找了一圈发现基于Nginx的OpenResty(Lua语言)扩展模块Orange挺好(也找了Kong,但是感觉复杂了点没用),还偷懒用Vagrant结合Docker来快速搭建环境,基于别人的Dockerfile把整个实验跑通了,觉得还不错.想着好像CoreOS是专门为Docker服务的,还买了一本<CoreOS实践>花小半天时间看完了,CoreOS在集群环境下确实很牛,但是我的环境还是轻量级点,所以还是基于CentOS来做,就这样研究了两天时间,

spark2.x由浅入深深到底系列六之RDD java api调用scala api的原理

RDD java api其实底层是调用了scala的api来实现的,所以我们有必要对java api是怎么样去调用scala api,我们先自己简单的实现一个scala版本和java版本的RDD和SparkContext 一.简单实现scala版本的RDD和SparkContext class RDD[T](value: Seq[T]) {   //RDD的map操作   def map[U](f: T => U): RDD[U] = {     new RDD(value.map(f))   

heroku create ENOTFOUND: getaddrinfo ENOTFOUND api.heroku.com api.heroku.com:443

$heroku create出现下面的错误: Creating app... !!! ? ENOTFOUND: getaddrinfo ENOTFOUND api.heroku.com api.heroku.com:443 2016/03/17 15:20:05 Error: getaddrinfo ENOTFOUND api.heroku.com api.heroku.com:443 at errnoException (dns.js:26:10) at GetAddrInfoReqWrap.

HTML5之本地文件系统API - File System API

HTML5之本地文件系统API - File System API 2014-06-03 17:54 19991人阅读 评论(0) 收藏 举报 目录(?)[+] 新的HTML5标准给我们带来了大量的新特性和惊喜,例如,画图的画布Canvas,多媒体的audio和video等等.除了上面我们提到的,还有比较新的特性 - File System API,它能够帮助我们来突破沙箱访问我们本地的文件系统,从而有效的弥补桌面和web应用之间的鸿沟.在今天这篇文章中,我们将会介绍基本的File system