swagger-to-graphql-colinyoung

1.6.1 • Public • Published

Swagger2GraphQL

Swagger2GraphQL converts your existing Swagger schema to GraphQL types where resolvers perform HTTP calls to certain real endpoints. It allows you to move your API to GraphQL with nearly zero effort and maintain both REST and GraphQL APIs.

Why?

Looking for contributors!

Usage

Basic server

const express = require('express');
const app = express();
const graphqlHTTP = require('express-graphql');
const graphQLSchema = require('swagger-to-graphql');

const proxyUrl = 'http://petstore.swagger.io/v2';
const pathToSwaggerSchema = './petstore.json';
const customHeaders = {
  // Authorization: 'Basic YWRkOmJhc2ljQXV0aA=='
}

graphQLSchema(pathToSwaggerSchema, proxyUrl, customHeaders).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:

  • pathToSwaggerSchema (string) is a path to your local swagger schema file. required
  • proxyUrl (string) base URL which will be used to hit your HTTP API. Can be taken either from Swagger schema baseUrl configuration or from this parameter.
  • customHeaders (object) key value object of custom headers, which should be included to the HTTP request. Can be used for example for authorization (look at the example above)

CLI usage

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

npm i -g swagger-to-graphql
# Valid `options` should be added as a comma separated list (case insensitive).
# Current options are listed below.
swagger-to-graphql --swagger=/path/to/swagger_schema.json --options=Opt1,Opt2 > ./types.graphql

CLI options (case insensitive)

  • allow-nan - instead of Float types that are exclusively just a valid Float or null, use a scalar type FloatOrNaN which can be a valid Float, the string value "NaN", or null.
  • empty-to-json - when a field has no type or is empty, instead of generating an object with a empty: String value, return a more flexible JSON type.
  • Others TBD

Video from O.J. Sousa Rodrigues at Vienna.JS

Build Status

Readme

Keywords

Package Sidebar

Install

npm i swagger-to-graphql-colinyoung

Weekly Downloads

5

Version

1.6.1

License

MIT

Unpacked Size

26.8 kB

Total Files

9

Last publish

Collaborators

  • colinyoung