当前位置: 首页 > 软件库 > 大数据 > 数据查询 >

swagger-to-graphql

Swagger to GraphQL API adapter
授权协议 MIT License
开发语言 Java
所属分类 大数据、 数据查询
软件类型 开源软件
地区 不详
投 递 者 龙浩博
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Swagger-to-GraphQL

Swagger-to-GraphQL converts your existing Swagger schema to an executableGraphQL schema where resolvers perform HTTP calls to certain real endpoints. Itallows you to move your API to GraphQL with nearly zero effort and maintain bothREST and GraphQL APIs. Our CLI tool also allows you get the GraphQL schema inSchema Definition Language.

Try it online! You can paste inthe url to your own Swagger schema. There are also public OpenAPI schemasavailable in the APIs.guru OpenAPI directory.

Features

  • Swagger (OpenAPI 2) and OpenAPI 3 support
  • Bring you own HTTP client
  • Typescript types included
  • Runs in the browser
  • Formdata request body
  • Custom request headers

Usage

Basic server

This library will fetch your swagger schema, convert it to a GraphQL schema andconvert GraphQL parameters to REST parameters. From there you are control ofmaking the actual REST call. This means you can reuse your existing HTTP client,use existing authentication schemes and override any part of the REST call. Youcan override the REST host, proxy incoming request headers along to your RESTbackend, add caching etc.

import express, { Request } from 'express';
import graphqlHTTP from 'express-graphql';
import { createSchema, CallBackendArguments } from 'swagger-to-graphql';

const app = express();

// Define your own http client here
async function callBackend({
  context,
  requestOptions,
}: CallBackendArguments<Request>) {
  return 'Not implemented';
}

createSchema({
  swaggerSchema: `./petstore.yaml`,
  callBackend,
})
  .then(schema => {
    app.use(
      '/graphql',
      graphqlHTTP(() => {
        return {
          schema,
          graphiql: true,
        };
      }),
    );

    app.listen(3009, 'localhost', () => {
      console.info('http://localhost:3009/graphql');
    });
  })
  .catch(e => {
    console.log(e);
  });

Constructor (graphQLSchema) arguments:

export interface Options<TContext> {
  swaggerSchema: string | JSONSchema;
  callBackend: (args: CallBackendArguments<TContext>) => Promise<any>;
}
  • swaggerUrl (string) is a path or URL to your swagger schema file. required
  • callBackend (async function) is called with all parameters needed to make aREST call as well as the GraphQL context.

CLI usage

You can use the library just to convert schemas without actually running server

npx swagger-to-graphql --swagger-schema=/path/to/swagger_schema.json > ./types.graphql

Apollo Federation

Apollo federation support can be added by usinggraphql-transform-federation.You can extend your swagger-to-graphql schema with other federated schemas orthe other way around. See thedemo with a transformed schemafor a working example.

Defining your HTTP client

This repository has:

To get started install node-fetch and copy thenode-fetch example into your server.

npm install node-fetch --save

Implementing your own HTTP client

There a unit test for our HTTP client example,it might be useful when implementing your own client as well.

The function callBackend is called with 2 parameters:

  • context is your GraphQL context. For express-graphql this is the incomingrequest object by default.Read more. Use this ifyou want to proxy headers like authorization. For exampleconst authorizationHeader = context.get('authorization').
  • requestOptions includes everything you need to make a REST call.
export interface CallBackendArguments<TContext> {
  context: TContext;
  requestOptions: RequestOptions;
}

RequestOptions

export interface RequestOptions {
  baseUrl?: string;
  path: string;
  method: string;
  headers?: {
    [key: string]: string;
  };
  query?: {
    [key: string]: string | string[];
  };
  body?: any;
  bodyType: 'json' | 'formData';
}
  • baseUrl like defined in your swagger schema: http://my-backend/v2
  • path the next part of the url: /widgets
  • method HTTP verb: get
  • headers HTTP headers which are filled using GraphQL parameters:{ api_key: 'xxxx-xxxx' }. Note these are not the http headers sent to theGraphQL server itself. Those will be on the context parameter
  • query Query parameters for this calls: { id: 123 }. Note this can be anarray. You can find some examples on how to deal with arrays in queryparameters in theqs documentation.
  • body the request payload to send with this REST call.
  • bodyType how to encode your request payload. When the bodyType isformData the request should be URL encoded form data. Ensure your HTTPclient sends the right Content-Type headers.

Resources

  • 反射与动态代理实践 简介 代码只是例子,具体看GitHub。有收获的请为GitHub的项目点个赞。 因为可能存在潜在的性能问题,本方案不推荐拥有大并发的接口采用,并且GitHub仓库已经更新为更加自动化/高效的实现。 禁止转载!本文已经发布在微信公众号ScalaCoder,以及个人博客https://dreamylost.cn https://github.com/growingio/growin

  • 最小的linux服务器 Today in web development, we will be learning how to: 在Web开发的今天,我们将学习如何: Easily setup a GraphQL Server with NodeJS 使用NodeJS轻松设置GraphQL服务器 Mock data without a database using json-server 使用j

  • 我在如何使用Gatsby建立博客 / How to build a blog with Gatsby这篇文章中提过GraphQL在Gatsby中的应用。总的来讲,它是一个新潮的技术,在适宜的使用场景威力无穷。这里我们来讨论一下用/不用GraphQL的理由吧。 简单介绍GraphQL  GrahQL GraphQL是Facebook2015年开源的数据查询规范。现今的绝大多数Web Service都

 相关资料
  • swagger-to-existing-nodejs-project Demo application that shows how to add Swagger UI Spec to existing Node.js/Express.js project Requirements Node.js v6.11.0 MongoDB v3.4.6 Gulp v3.9.1 Usage Install d

  • 1 历史、现状和发展 Swagger:是一个规范和完整的框架,可以用于生成、描述、调用和可视化 RESTful 风格的 Web 服务,总体目标是使客户端和文档系统与服务器以同样的速度进行更新。Swagger倾向于在线测试接口和数据。并且这是一个完全开源的项目,并且它也是一个基于Angular的成功案例,我们可以下载源码并自己部署它,也可以修改它或集成到我们自己的软件中。 Swagger的创始人:

  • Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。 总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 API 来始终保持同步。Swagger 让部署管理和使用功能强大的 API 从未如此简单。

  • 当我在网上做研究时,似乎人们也在这个堆栈中使用SQS,SNS会将信息放在SQS上,然后SQS会调用Lambda。 我想我想理解的是在这方面对SQS的需求。这增加了什么价值?换句话说,直接从SNS调用我的Lambda会失去什么?

  • .to( target:Object, duration:Number, vars:Object, position:* ) : * 添加一个TweenLite.to()动画到时间轴,相当于add(TweenLite.to(...)),以下两行产生相同的结果: myTimeline.add( TweenLite.to(element, 1, {left:100, opacity:0.5}) );

  • 我正在测试在服务器上设置一个Git(EC2-Linux,Amazon)。一切都很好,但我做了一件事,我不能复制,我想理解。 我正在遵循以下Git在服务器https://git-scm.com/book/en/v2/git-on-the-server-setting-up-the-server上的教程 我创建了4个用户进行测试,并在服务器上创建了他们的ssh密钥。(User Git)在/home/G