Skip to content
GraphQL binding implementation for OpenAPI/Swagger endpoints
TypeScript JavaScript
Branch: master
Clone or download
schickling Merge pull request #7 from graphql-binding/renovate/kbrandwijk-swagge…

fix(deps): update dependency @kbrandwijk/swagger-to-graphql to v2.4.3
Latest commit 77b38b4 Feb 23, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci feat: initial implementation Jan 28, 2018
example docs: updated readme and examples Jan 29, 2018
src feat: initial implementation Jan 28, 2018
.gitignore feat: initial implementation Jan 28, 2018
.npmignore feat: initial implementation Jan 28, 2018
LICENSE Create LICENSE Feb 23, 2018 docs(readme): added explanation about schema transformation Jan 29, 2018
package.json fix(deps): update dependency @kbrandwijk/swagger-to-graphql to v2.4.3 Feb 23, 2018
tsconfig.json feat: initial implementation Jan 28, 2018
yarn.lock fix(deps): update dependency @kbrandwijk/swagger-to-graphql to v2.4.3 Feb 23, 2018

GraphQL Binding for Swagger/OpenAPI

CircleCI npm version

Embed any Swagger/OpenAPI endpoint as GraphQL API into your server application.

Both yaml and json specifications are supported.


yarn add graphql-binding-openapi

How it works

A service endpoint that uses the Swagger/OpenAPI specification contains a definition file (in either JSON or YAML format). This definition file has the following structure:

    "swagger": "2.0",
    "info": { },
    "host": "",
    "basePath": "/v2",
    "tags": [ ],
    "schemes": [ "http" ],
    "paths": { },
    "securityDefinitions": {},
    "definitions": { },
    "externalDocs": { }

An example for the petstore endpoint can be found here.

This endpoint definition is transformed into a GraphQL schema, with all the paths from the endpoint translated into queries and mutations. The query and mutation names are based on the unique operationName found in the definition for each path. This schema looks like this:

type Query {
  # GET /pet/findPetsByStatus
  findPetsByStatus(status: [String]): [findPetsByStatus_items]

  # GET /pet/findPetsByTags
  findPetsByTags(tags: [String]): [findPetsByTags_items]

  # GET /pet/{petId}
  getPetById(petId: String): getPetById

  # other queries

type Mutation {
  # POST /pet
  addPet(body: param_addPet_body): addPet
  # PUT /pet
  updatePet(body: param_updatePet_body): updatePet
  # PUT /pet/{petId}
  updatePetWithForm(petId: String, name: String, status: String): updatePetWithForm
  # DELETE /pet/{petId}
  deletePet(api_key: String, petId: String): deletePet
  # other mutations

The full schema for the petstore endpoint can be found here.

The remote executable GraphQL schema (containing all the resolvers for querying the original endpoint) is exposed as a binding by graphql-binding-openapi, making each query and mutation available as a method on the binding class, for example:

petstore.query.findPetsByStatus({ status: "available" })
petstore.mutation.addPet({ /* mutation arguments */ })



See example directory for a standalone example.

With graphql-yoga

const { OpenApi } = require('graphql-binding-openapi')
const { GraphQLServer } = require('graphql-yoga')

const resolvers = {
  Query: {
    findAvailablePets: async (parent, args, context, info) => {
      return context.petstore.query.findPetsByStatus({ status: "available" }, context, info)

const server = new GraphQLServer({ 
  context: async req => {
    petstore: await OpenApi.init('./petstore.json', '')

server.start(() => console.log('Server running on http://localhost:4000'))

graphql-config support

OpenAPI bindings are supported in graphql-config using its extensions model. OpenAPI bindings can be added to the configuration like this:

    schemaPath: src/generated/petstore.graphql
        definition: petstore.json

This will enable running graphql get-schema to output the generated schema from the definition to the defined schemaPath.

Static bindings

Static binding support coming soon.

You can’t perform that action at this time.