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-graphqlnode

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 包git

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

    相似,只是步驟多了幾步github

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/harborgraphqldocker

相關文章
相關標籤/搜索