graphql-binding-openapi
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

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.

Install

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": "petstore.swagger.io",
    "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 */ })

Example

Standalone

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({ 
  resolvers, 
  typeDefs,
  context: async req => {
    ...req,
    petstore: await OpenApi.init('./petstore.json', 'http://petstore.swagger.io/v2')
  }
})

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:

projects:
  petstore:
    schemaPath: src/generated/petstore.graphql
    extensions:
      openapi:
        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.

Readme

Keywords

none

Package Sidebar

Install

npm i graphql-binding-openapi

Weekly Downloads

0

Version

1.0.5

License

MIT

Unpacked Size

41.2 kB

Total Files

8

Last publish

Collaborators

  • kbrandwijk