GithubHelp home page GithubHelp logo

kandros / graphql-binding-openapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from graphql-binding/graphql-binding-openapi

0.0 2.0 0.0 56 KB

GraphQL binding implementation for OpenAPI/Swagger endpoints

License: MIT License

JavaScript 36.95% TypeScript 63.05%

graphql-binding-openapi's Introduction

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.

graphql-binding-openapi's People

Contributors

kbrandwijk avatar renovate-bot avatar schickling avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.