Skip to content

graphql-binding/swagger-to-graphql

This branch is 3 commits ahead of, 233 commits behind yarax/swagger-to-graphql:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

cfc2f39 · Feb 23, 2018

History

53 Commits
Aug 13, 2017
Jun 15, 2017
Feb 23, 2018
Feb 23, 2018
Jun 27, 2017
Jul 3, 2017
Apr 11, 2017
Jun 27, 2017
Jun 24, 2017
Jan 11, 2018
Feb 23, 2018
Feb 23, 2018

Repository files navigation

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 afford and maintain both: REST and GraphQL APIs.

Why?

Usage

Basic server

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

graphQLSchema('./petstore.json').then(schema => {
  app.use('/graphql', graphqlHTTP(() => {
    return {
      schema,
      context: {
        GQLProxyBaseUrl: API_BASE_URL
      },
      graphiql: true
    };
  }));

  app.listen(3009, 'localhost', () => {
    console.info(`API is here localhost:3009/graphql`);
  });
}).catch(e => {
  throw e;
});

CLI convertion

npm i -g swagger-to-graphql
swagger-to-graphql --swagger=/path/to/swagger_schema.json > ./types.graphql

Authorization

Basic Auth example:

 ...
  context: {
    GQLProxyBaseUrl: API_BASE_URL,
    headers: {
      Authorization: 'Basic YWRkOmJhc2ljQXV0aA==',
      "X-Custom": 'customValue'
    }
    BearerToken: req.get('authorization')
  },
 ...

Bearer Token example:

 ...
  context: {
    GQLProxyBaseUrl: API_BASE_URL,
    headers: {
      Authorization: req.get('authorization'),
      "X-Custom": 'customValue'
    }
  },
 ...

All context options